发送Email并添加附件

1. 添加命名空间

using System.Net.Mail;

using System.Net;

2. The HTML MarpUp

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <table>

                <tr>

                    <td>

                        <asp:Label ID="Label1" runat="server" Text="From:"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="txtFrom" runat="server" Height="17px" Width="231px"></asp:TextBox>

                    </td>

                </tr>



                <tr>

                    <td>

                        <asp:Label ID="Label3" runat="server" Text="To:"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="txtTo" runat="server" Height="19px" Width="226px"></asp:TextBox>

                    </td>

                </tr>



                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Subject:"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="txtSubject" runat="server" Height="19px" Width="240px"></asp:TextBox>

                    </td>

                </tr>



                <tr>

                    <td>

                        <asp:Label ID="Label4" runat="server" Text="Body:"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="102px" Width="234px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label5" runat="server" Text="Accachment:"></asp:Label>

                    </td>

                    <td>

                        <asp:FileUpload ID="FileUpload1" runat="server" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="text-align:center">

                        <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>
View Code

3. 封装了一个发送Email的方法,根据此方法添加相应的参数,即可进行发功邮件

  protected void SendMail(string FromAddress, string ToAddress, string Subject, string Body, string UserName, string Password)

        {

            // smtp settings

            SmtpClient smtp = new SmtpClient();

            {

                smtp.Host = "smtp.gmail.com";

                smtp.Port = 587;

                smtp.EnableSsl = true;

                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                smtp.Credentials = new NetworkCredential(UserName, Password);

                smtp.Timeout = 20000;

            }



            //mail settings

            MailMessage mail = null;

            if (FileUpload1.HasFile)

            {

                mail = new MailMessage(FromAddress, ToAddress, Subject, Body);

                //add attachment file

                String FileName = FileUpload1.PostedFile.FileName;

                System.Net.Mail.Attachment attachment;

                attachment = new System.Net.Mail.Attachment(FileName);

                mail.Attachments.Add(attachment);

            }

            smtp.Send(mail);

        }

 

More relevant information:

http://www.developer.com/net/csharp/article.php/3287361/Using-ASPNET-to-Send-E-MailmdashIncluding-Attachments.htm

http://www.aspdotnet-suresh.com/2010/12/how-to-send-mail-using-gmail.html

http://forums.asp.net/t/1691720.aspx?TIME+OUT+ERROR+

http://www.codeproject.com/Answers/321575/sending-email-to-gmail-from-asp-net#answer3

http://www.codeproject.com/Questions/582062/Gettingplusanpluserrorplus-OperationplusTimedpluso

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.timeout(v=vs.110).aspx

你可能感兴趣的:(email)