使用GMAIL寄信
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class sendGmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("寄件者帳號@gmail.com", "寄件者顯示名稱"); //
msg.To.Add(new MailAddress("收件人的MAIL帳號@livemail.tw", "收件者顯示名稱")); //可寫迴圈多筆寄送
msg.Subject="主旨";
msg.IsBodyHtml=true;
string str ="<h2>內文1</h2>";
str += "<h3>內文2</h3>";
str += "<h3>543</h3>"; // html上的東西都能收,圖檔要注意路徑,http://網址/資料夾/檔名,也能放超連結
msg.Body = str;
// msg.Attachments //附件
// msg.CC //副本
SmtpClient clinet= new SmtpClient("smtp.gmail.com");
////可用asp.net組態,管理工具設定,下面可以不用自己寫,但要手動去 Web.config 加上 enableSsl = true;
NetworkCredential loginInfo = new NetworkCredential("gmail帳號@gmail.com", "gmail帳號的密碼");
clinet.UseDefaultCredentials = false;
clinet.Credentials = loginInfo;
clinet.EnableSsl = true;
//// clinet.Send("寄件者帳號@gmail.com", "收件者帳號@livemail.tw", "主旨", "內文"); //單筆方式
clinet.Send(msg);
}
}
////Web.config會加上這段,自己加上 enableSsl = "true"
<!--
</connectionStrings><system.net>
<mailSettings>
<smtp from="寄件者的顯示@gmail.com">
<network host="smtp.gmail.com" password="gamil密碼" userName="gmail帳號" enableSsl = "true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
-->
沒有留言:
張貼留言