[セットトップ]asp.Netメール送信通知機能(受信者はQQメールボックスなどを含む)

5933 ワード


1、共通のメール送信操作クラスを作成する:MailHelper.cs、コードは以下の通りです.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;

/// <summary>
///MailHelper      
/// </summary>
public class MailHelper
{
 public MailHelper()
 {
  //
  //TODO:            
  //
 }

    /// <summary>
    ///       
    /// </summary>
    /// <param name="Addressee">     </param>
    /// <param name="From">     </param>
    /// <param name="sendpassword">     </param>
    /// <param name="Copy">     </param>
    /// <param name="secret">     </param>
    /// <param name="Subject">    </param>
    /// <param name="Attachment">    </param>
    /// <param name="Body">    </param>
    public string SendeEmail(string Addressee, string From, string sendpassword, string Copy, string secret, string Subject, string Attachment, string Body)
    {
        MailMessage objMailMessage;
        MailAttachment objMailAttachment;


        //       
        objMailMessage = new MailMessage();

        //   EMAIL
        objMailMessage.From = From;//     

        //   EMAIL
        objMailMessage.To = Addressee; //      
        //    
        objMailMessage.Cc = Copy;
        //  misong
        objMailMessage.Bcc = secret;


        //    
        objMailMessage.Subject = Subject; //       

        //    
        objMailMessage.Body = Body;//       

        //         
        if (Attachment != "")
        {
            objMailAttachment = new MailAttachment(Attachment);//        c:\\test.txt
            objMailMessage.Attachments.Add(objMailAttachment);//             
        }

        //    SMTP     ,    Microsoft .NET Framework SDK v1.1       
        //    
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        //   
        string name = From.Substring(0, From.IndexOf('@'));
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", name);
        //  
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendpassword);
        //          ,         :                。      : 554 : Client host rejected: Access denied
        //SMTP        
        string smtp = "smtp." + From.Substring(From.IndexOf('@') + 1);
        SmtpMail.SmtpServer = "smtp." + From.Substring(From.IndexOf('@') + 1);
        //      

        try
        {
            SmtpMail.Send(objMailMessage);
            return "      !";
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            return ex.Message;
        }
        //      
    }


}

2、Webページの使用及び呼び出し例、コードは以下の通りである.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MailSendDemo.aspx.cs" Inherits="demo_MailSendDemo" ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        //    
        function getAttachFile() {
            var file_value = document.getElementById("File1").value;
            document.getElementById("HiddenField1").value = file_value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
          :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
          :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
          :<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
          :<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
          :<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
          :<input id="File1" type="file" />
        <br />
        <asp:Button ID="btnSend" runat="server" Text="  " OnClientClick="getAttachFile()" OnClick="btnSend_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    </form>
</body>
</html>



 string a = mails.SendeEmail(TextBox 1.Text、「あなたのメール番号」、「メールボックスパスワード」、TextBox 2.Text、TextBox 4.Text、TextBox 5.Text、filePath、TextBox 3.Text);
ここでは、実際に使用するときは、送信者のメールアドレスとパスワードに置き換えてください.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class demo_MailSendDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSend_Click(object sender, EventArgs e)
    {       

        //       ,       
        MailHelper mails = new MailHelper();

        string filePath = HiddenField1.Value;

        string a = mails.SendeEmail(TextBox1.Text, "      ", "    ", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);

        Label1.Text = a;
    }
}

===========================================================================
もしあなたに役に立つと思ったら、微信スキャンでサポートします.