C#

C#实现邮件发送

这是很早的一个例子,发送邮件,基于C#,

1.MailUtil,邮件的函数类

using System.Collections.Generic;
using System.Net.Mail;
using System.Configuration;

public class MailUtil
{
private SmtpClient smtp = null;

private string FromUser = null;

public MailUtil()
{
string _host=ConfigurationManager.AppSettings[“MailServer”].ToString().Split(‘:’)[0];
int _port = Convert.ToInt16(ConfigurationManager.AppSettings[“MailServer”].ToString().Split(‘:’)[1]);
smtp = new SmtpClient(_host, _port);
FromUser = ConfigurationManager.AppSettings[“FromUsr”].ToString();
}

public MailMessage BuildMessage(List<string> lstToAddress, List<string> lstCCAddress, string strSubject, string strBody, string strDislayName)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(FromUser, strDislayName);
foreach (string strToAddress in lstToAddress)
{
message.To.Add(new MailAddress(strToAddress));
}
foreach (string strCCAddress in lstCCAddress)
{
message.CC.Add(new MailAddress(strCCAddress));
}
message.Subject = strSubject;
message.Body = strBody;
message.IsBodyHtml = true;
return message;
}

/// <summary>
/// 方法的重载
/// </summary>
/// <param name=”lstToAddress”></param>
/// <param name=”lstCCAddress”></param>
/// <param name=”strSubject”></param>
/// <param name=”strBody”></param>
/// <returns></returns>
public MailMessage BuildMessage(List<string> lstToAddress, List<string> lstCCAddress, string strSubject, string strBody)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(FromUser, “XXX提醒”);
foreach (string strToAddress in lstToAddress)
{
message.To.Add(new MailAddress(strToAddress));
}
foreach (string strCCAddress in lstCCAddress)
{
message.CC.Add(new MailAddress(strCCAddress));
}

message.Subject = strSubject;
message.Body = strBody;
message.IsBodyHtml = true;
return message;
}

public void SendMail(MailMessage message)
{
try
{
if (smtp != null)
{
smtp.Send(message);
}
}
catch (Exception ex)
{
throw ex;
}
}
}

2.MdlMail,邮件的Model

public class MdlMail
{
public string Subject { get; set; }
public string Body { get; set; }
}

3.在Config文件里面配置邮件服务器的端口和发送人员的Mail 地址

<appSettings>
<add key=”MailServer” value=”hostip:port” />
<add key=”FromUsr” value=”sender@xxx.com”/>
</appSettings>

4.Mail邮件发送的实现

private void btnSend_Click(object sender, EventArgs e)
{
if (!IsComponetNotNull())
{
MessageBox.Show(“Not Empty!”);
return;
}

try
{
MailUtil mail = new MailUtil();
MdlMail MailInfo = new MdlMail();
MailInfo.Subject = txtSubject.Text.Trim();
MailInfo.Body = txtBody.Text.Trim();

List<string> MailTo = new List<string>();

foreach (string item in txtTo.Text.Split(‘,’))
{
MailTo.Add(item);
}

List<string> MailCc = new List<string>();
foreach (string item in txtCC.Text.Split(‘,’))
{
MailCc.Add(item);
}

System.Net.Mail.MailMessage mailmessage = mail.BuildMessage(MailTo, MailCc, MailInfo.Subject, MailInfo.Body);
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mail.SendMail(mailmessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

Pls call me CPP.
Posts created 150

发表评论

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top