110,499
社区成员
发帖
与我相关
我的任务
分享
public static string Send() {
string result = "";
try {
MailMessage mail = new MailMessage("duling509@163.com", "duling803@163.com", "主题", "内容");
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.163.com";//如果使用163发送
smtp.Credentials = new System.Net.NetworkCredential("duling509@163.com", "7889698");//邮箱账号与密码
smtp.Send(mail);
}
catch (Exception ex) {
result = ex.Message;
}
return result;
}
namespace ASB.Test.Service.MailService
{
public class MailSender
{
private static string SMTPServer = AppSettings["SMTPServer"];
private static int SMTPPort = int.Parse(AppSettings["SMTPPort"]);
private static string SMTPFrom = AppSettings["From"];
private static string SMTPFromPwd = AppSettings["FromPwd"];
private static string SMTPFromDisplay = AppSettings["FromDisplay"];
private static System.Collections.Specialized.NameValueCollection AppSettings { get { return System.Configuration.ConfigurationManager.AppSettings; } }
public static bool SendEmail(string from, string fromDisplay, string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
{
bool isSuccess = false;
try
{
SmtpClient client = new SmtpClient(SMTPServer);
client.Credentials = new System.Net.NetworkCredential(SMTPFrom, SMTPFromPwd);
//client.EnableSsl = true;
MailAddress fromAddr = new MailAddress(from, fromDisplay);
// Set destinations for the e-mail message.
MailAddress toAddr = new MailAddress(to, toDisplay);
// Specify the message content.
MailMessage message = new MailMessage(fromAddr, toAddr);
if (cc != string.Empty)
{
MailAddress ccAddr = new MailAddress(cc, ccDisplay);
message.CC.Add(ccAddr);
}
message.IsBodyHtml = true;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
if (attachments != null)
{
foreach (MailAttachment mailAttach in attachments)
{
if (mailAttach.AttachmentData != null)
{
Attachment attach = new Attachment(mailAttach.AttachmentData, mailAttach.AttachmentName);
message.Attachments.Add(attach);
}
}
}
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
message.Dispose();
isSuccess = true;
return isSuccess;
}
catch (Exception ex)
{
try
{
isSuccess = false;
}
catch
{
isSuccess = false;
}
return isSuccess;
}
}
public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body)
{
return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body,null);
}
public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
{
return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body, attachments);
}
}
}