邮件发送!(2种)
SMTP方式:
public void SendMail()
??{
???try
???{
????MailMessage MM=new MailMessage();//4個textbox
????MM.From=this.text_fromMail.Text; //發送方地址
????MM.To=this.text_toMail.Text; //接收方地址
????MM.Subject=this.text_Name.Text; //主題
????MM.Body=this.text_Messages.Text; //內容
??
????//設置為需要用戶驗證
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");??
????//設置驗證用戶名(把emailaddress改為你的驗證用戶名)
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "gaoc00");?
????//設置驗證密碼(把password改為你的驗證密碼)
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456");
????SmtpMail.SmtpServer = "smtp.163.com";? //郵件服務器地址
????SmtpMail.Send(MM);
???}
???catch(Exception ex)
???{
????throw (ex);
???}
??}
jmail方式:
public bool SendEmail(bool HtmlFormat)
??{
???bool isOk = false;
???jmail.MessageClass objMessage = new MessageClass();
???objMessage.Silent = true;
???objMessage.Logging = true;
???objMessage.Charset = "GB2312";
???if(HtmlFormat == true)
???{
????objMessage.ContentType = "text/html";
????objMessage.HTMLBody = "這里設置郵件內容";
???}
???else
???{
????objMessage.ContentType = "text/plain";
????objMessage.Body = "這里設置郵件內容";
???}
???objMessage.AddRecipient("gc_12345@163.com","","");//收件人地址
???objMessage.FromName= "bmyr";
???objMessage.From = "bmyr1234@21cn.com";//發件人地址
???objMessage.MailServerUserName = "bmyr";
???objMessage.MailServerPassWord = "123456";
???objMessage.Subject = "這里設置主題";
???//--執行發送
???try
???{
????isOk=objMessage.Send("smtp.21cn.com",false);
????if(isOk)
????{
?????objMessage.Close();
?????return true;
????}
????else
????{?????
?????objMessage.Close();
?????return false;
????}
???}
???catch(Exception objExp)
???{
????objMessage.Close();
????return false;
???}????
??}
調用:
???SendMail();
???SendEmail(true);
public void SendMail()
??{
???try
???{
????MailMessage MM=new MailMessage();//4個textbox
????MM.From=this.text_fromMail.Text; //發送方地址
????MM.To=this.text_toMail.Text; //接收方地址
????MM.Subject=this.text_Name.Text; //主題
????MM.Body=this.text_Messages.Text; //內容
??
????//設置為需要用戶驗證
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");??
????//設置驗證用戶名(把emailaddress改為你的驗證用戶名)
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "gaoc00");?
????//設置驗證密碼(把password改為你的驗證密碼)
????MM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456");
????SmtpMail.SmtpServer = "smtp.163.com";? //郵件服務器地址
????SmtpMail.Send(MM);
???}
???catch(Exception ex)
???{
????throw (ex);
???}
??}
jmail方式:
public bool SendEmail(bool HtmlFormat)
??{
???bool isOk = false;
???jmail.MessageClass objMessage = new MessageClass();
???objMessage.Silent = true;
???objMessage.Logging = true;
???objMessage.Charset = "GB2312";
???if(HtmlFormat == true)
???{
????objMessage.ContentType = "text/html";
????objMessage.HTMLBody = "這里設置郵件內容";
???}
???else
???{
????objMessage.ContentType = "text/plain";
????objMessage.Body = "這里設置郵件內容";
???}
???objMessage.AddRecipient("gc_12345@163.com","","");//收件人地址
???objMessage.FromName= "bmyr";
???objMessage.From = "bmyr1234@21cn.com";//發件人地址
???objMessage.MailServerUserName = "bmyr";
???objMessage.MailServerPassWord = "123456";
???objMessage.Subject = "這里設置主題";
???//--執行發送
???try
???{
????isOk=objMessage.Send("smtp.21cn.com",false);
????if(isOk)
????{
?????objMessage.Close();
?????return true;
????}
????else
????{?????
?????objMessage.Close();
?????return false;
????}
???}
???catch(Exception objExp)
???{
????objMessage.Close();
????return false;
???}????
??}
調用:
???SendMail();
???SendEmail(true);
轉載于:https://www.cnblogs.com/wzhxj/archive/2006/01/06/312450.html
總結
- 上一篇: 做一个中国式站长必须知道的五十个问题
- 下一篇: 新手学习编程如何更加轻松的学习C/C++