纳金网
标题:
邮件发送专用代码
[打印本页]
作者:
may
时间:
2016-2-28 00:37
标题:
邮件发送专用代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Net;
namespace fw.fwEmail
{
public class FWEMailHelper
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">收件人邮箱</param>
/// <param name="emailAddress">发件人邮箱地址</param>
/// <param name="emailUid">发件人邮箱账号</param>
/// <param name="emailPwd">发件人邮箱密码</param>
/// <param name="emailSMTP">SMTP 例:smtp.163.com,smtp.qq.com</param>
/// <param name="title">邮件标题</param>
/// <param name="content">邮件内容</param>
public void sendEmail(string mail, string emailAddress, string emailUid, string emailPwd, string emailSMTP, string title, string content)
{
MailMessage mailMsg = new MailMessage();//两个类,别混了,要引入System.Net这个Assembly
mailMsg.From = new MailAddress(emailAddress, "系统邮件");//源邮件地址
mailMsg.To.Add(new MailAddress(mail));//目的邮件地址。可以有多个收件人
mailMsg.Subject = title;//发送邮件的标题
StringBuilder builder = new StringBuilder();
builder.Append(content);
mailMsg.Body = builder.ToString();//发送邮件的内容
mailMsg.IsBodyHtml = true;//可以以HTML格式查看邮箱内容
SmtpClient client = new SmtpClient(emailSMTP);//smtp.163.com,smtp.qq.com
client.Credentials = new NetworkCredential(emailUid, emailPwd);//填写登录邮箱的用户名与密码
client.Send(mailMsg);
}
/// <summary>
/// 发送SMTP邮件
/// </summary>
/// <param name="host">邮箱服务器(smtp.qq.com)</param>
/// <param name="port">邮箱服务器端口号</param>
/// <param name="fromEMail">发送者的邮箱(xxxxxx@shencai.cc)</param>
/// <param name="fromPassword">发送者的邮箱的密码(xxxxxx)</param>
/// <param name="toEMail">接收者者的邮箱(xxxxxx@shencai.cc)</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件内容</param>
public static bool SendEMailBySMTP(string host, int? port, string fromEMail, string fromPassword, string toEMail, string subject, string body)
{
//SendEMailBySMTP("smtp.qq.com", null , "xxxxxx@shencai.cc", "xxxxxx", "xxxxxx@163.com,xxxxxx@qq.com", "测试", "用asp.net发送邮件,用qq的smtp.qq.com服务器,测试成功");
bool Result;
SmtpClient client;
if (port.HasValue)
{
client = new SmtpClient(host, port.Value);
}
else
{
client = new SmtpClient(host);
}
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromEMail, fromPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage(fromEMail, toEMail, subject, body);
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.IsBodyHtml = true;
try
{
client.Send(mailMessage);
Result = true;
}
catch
{
Result = false;
}
return Result;
}
}
}
复制代码
欢迎光临 纳金网 (http://old.narkii.com/club/)
Powered by Discuz! X2.5