asp.net 發送郵件

來源:互聯網
上載者:User
  

   string file = Server.MapPath("email_test.txt");

            System.Web.Mail.MailMessage mes = new System.Web.Mail.MailMessage();

            mes.To = "發送郵箱";
            mes.From = "接收郵箱";

          

            MailAttachment mailAttachment = new MailAttachment(file);

            mes.Attachments.Add(mailAttachment);
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//基本許可權 
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "發送郵箱名");
            mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "發送郵箱密碼");
            mes.BodyEncoding = System.Text.Encoding.UTF8;

            mes.Body = "郵件 本文 email content";

            //以下可以選擇文本轉譯格式

            mes.BodyFormat = MailFormat.Html;

            mes.Subject = "郵件發送測試";

            SmtpMail.SmtpServer = "smtp.163.com";
            SmtpMail.Send(mes);

            Response.Write("發送成功!");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

//以下是利伺服器pop3協議發送郵件

 try
        {

            string file = Server.MapPath("email_test.txt");

            MailAddress from = new MailAddress("zhengpengfei0791@163.com");//執行個體一個郵件發送地址
            MailAddress to = new MailAddress("solomon_msn@live.cn");//執行個體一個郵件接收地址

            MailMessage mes = new MailMessage(from, to);

            Attachment myAttachment = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);

            System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;

            disposition.CreationDate = System.IO.File.GetCreationTime(file);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

            mes.Attachments.Add(myAttachment);
            mes.IsBodyHtml = true;

            mes.Subject = "郵件測試";
            mes.Subject = "郵件內文 Email Content<img src='";

            SmtpClient client = new SmtpClient();
            client.Host = Request.UserHostAddress;
            client.Port = 25;

            client.Credentials = new System.Net.NetworkCredential("toEmail", "test");

            client.Send(mes);

            Response.Write("郵件發送成功!");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.