[ASP.NET] 寄送 EMail

  • 3408
  • 0
  • 2013-07-09

[ASP.NET] 寄送 EMail

此範例使用 Gmail 來做 SMTP Server 

另外,範例中將一些設定列出來,讓測試比較方便

使用的話,請根據需求再修訂一下。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Mail Demo </title>
    <style type="text/css">
        table
        {
            width: 100%;
        }
        input, span
        {
            width: 90%;
        }
        textarea
        {
            height: 100%;
            width: 90%;
        }
        tr:last-child td
        {
            height: 100px;
        }
        tr td:first-child
        {
            width: 20px;
        }
        td:nth-child(even)
        {
            width: 110px;
            padding: 0px;
            margin-left: 10px;
        }
        td:nth-child(odd)
        {
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        使用 Gmail
    </h3>
    <table>
        <tr>
            <td>
            </td>
            <td>
                <span>寄件人名稱:</span>
            </td>
            <td>
                <asp:TextBox ID="tbSenderName" runat="server"></asp:TextBox>
            </td>
            <td>
                <span>寄件人GMail:</span>
            </td>
            <td>
                <asp:TextBox ID="tbSenderMail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <span>寄件人帳號:</span>
            </td>
            <td>
                <asp:TextBox ID="tbSenderAccount" runat="server"></asp:TextBox>
            </td>
            <td>
                <span>寄件人密碼:</span>
            </td>
            <td>
                <asp:TextBox ID="tbSenderPwd" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <span>郵件主旨:</span>
            </td>
            <td>
                <asp:TextBox ID="tbMailSubject" runat="server"></asp:TextBox>
            </td>
            <td>
                <span>郵件位址:</span>
            </td>
            <td>
                <asp:TextBox ID="tbMail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                郵件內容:
            </td>
            <td>
                <asp:TextBox ID="tbContent" runat="server" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td>
                郵件附件:
            </td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnUP" runat="server" Text="UploadAttachment" 
                    onclick="btnUP_Click1" />
            </td>
        </tr>
    </table>
    <hr />
    <div>
        <asp:Button ID="btnSendMail" runat="server" Text="Send Mail" OnClick="btnSendMail_Click" /></div>
    </form>
</body>
</html>

內容


    static string hasfile = "";
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        //MailMessage MyMail = new MailMessage();
        using (MailMessage MyMail = new MailMessage())
        {
            MyMail.From = new MailAddress(tbSenderMail.Text, tbSenderName.Text);
            MyMail.To.Add(new MailAddress(tbMail.Text));
            MyMail.SubjectEncoding = Encoding.UTF8;
            MyMail.Subject = Title;
            MyMail.IsBodyHtml = true;
            MyMail.BodyEncoding = Encoding.UTF8;
            MyMail.Body = "<font size=\"2\" style=\"letter-spacing:1pt; line-height:1.8; font-family:Arial;\"  >" +
                          tbContent.Text + "<br/>" +
                          "===================================================</font>";

            //加入附件
            if (hasfile != "")
            {
                Attachment amAnnex = new Attachment(hasfile);
                MyMail.Attachments.Add(amAnnex);
            }

            //副本收件人
            //MyMail.CC.Add(new MailAddress("xxx@gmail.com"));
            //密件收件人
            //MyMail.Bcc.Add(new MailAddress("xxx@gmail.com"));

            //SMTP的方法傳送
            SmtpClient MySMTP = new SmtpClient();
            MySMTP.DeliveryMethod = SmtpDeliveryMethod.Network;

            //是否使用SSL
            MySMTP.EnableSsl = true;
            MySMTP.Credentials = new NetworkCredential(tbSenderAccount.Text, tbSenderPwd.Text);
            MySMTP.Host = "smtp.gmail.com";
            MySMTP.Port = 587;
            MySMTP.Send(MyMail);
        }
        //刪除附件檔案
        if (hasfile != "")
        {
            System.IO.File.Delete(hasfile);
            hasfile = "";
        }
    }

    protected void btnUP_Click1(object sender, EventArgs e)
    {
        //附件存檔
        if (FileUpload1.HasFile)
        {
            string strFullPath = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/" + FileUpload1.FileName);
            FileUpload1.SaveAs(strFullPath);
            hasfile = strFullPath;
        }
    }

image

image

範例檔案 下載