1、.net自带的
using System.Net.Mail;
string smtp = "发信人所用邮箱的服务器";
//发信人所用邮箱的服务器
string mailForm = "发件人的邮箱";
//发件人的邮箱
string mailPwd = "发件人的密码";
//发件人的密码
string mailTo = "收件人email";
//收件人信息
string mailTitle = "邮件标题";
//邮件标题
string mailContent = "邮件内容";
//邮件内容
SmtpClient client = new SmtpClient(smtp);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(用户名, mailPwd);//客户端身份验证机制
client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何处理待发的邮件
MailMessage message = new MailMessage(mailForm, mailTo, mailTitle, mailContent);
message.BodyEncoding = Encoding.Default;
message.IsBodyHtml = true;
client.Send(message);
2、使用jmail
在.net项目里,添加引用->COM 找到JMail 4.0 Library
jmail.Message emailMsg= new jmail.Message();
emailMsg.Body = "这里可以是用户填写的表单内容,可以取自Form。"
emailMsg.Charset = 字符集,缺省为"US-ASCII"
emailMsg.From = from;
emailMsg.ContentType = 信件的contentype,如果你以HTML格式发送邮件, 改为"text/html"即可
emailMsg.Encoding = 设置附件编码方式(缺省是"base64)
emailMsg.AddAttachment( "c:/anyfile.zip" ) 添加文件附件到邮件
emailMsg.AddRecipient(收件人)
emailMsg.MailServerUserName =用户名
emailMsg.MailServerPassWord = 密码
emailMsg.Subject = 主题;
emailMsg.Close() 强制JMail关闭缓冲的与邮件服务器的连接
emailMsg.Send("smtp.163.com")发送
3、使用CDO.Message
在C:/WINDOWS/system32/下有个叫cdosys.dll的动态链接库文件,将它复制出来,并在你的程序中引用它
CDO.Message对象:
CDO.Message emailMsg = new Message();
emailMsg.Form:发件人邮件地址
emailMsg.To:收件人邮件地址
emailMsg.Subject:邮件标题
emailMsg.HtmlBody:邮件主体,比如"<html><body>" + "邮件信息" + "</body></html>";
emailMsg.AddAttachment():添加附件
emailMsg.Send():发送邮件
配置设置:
CDO.IConfiguration iConfg = emailMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 发送端口;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = 登陆名@服务器地址
oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = 登陆名@服务器地址
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = 用户名
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = 密码
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = SMTP服务器端口;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = SMTP服务器
oFields.Update();