MailMessage mail = new MailMessage(); mail.To = "[email protected]"; mail.From = "[email protected]"; mail.Subject = "this is a test email."; mail.Body = "Some text goes here"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here SmtpMail.SmtpServer = "mail.mycompany.com"; //your real server goes here SmtpMail.Send( mail );
cdonts和jmail
cdonts是自带的.jmail是第三方
把jmail引用到工程中,加入jmail的命名空间,在这里我将引用jmail包的MessageClass类。
Logging属性:是否使用日志
Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
MailServerUserName属性:发信人的用户名,如:[email protected];
MailServerPassWord属性:发信人的密码
From属性:发信人
Subject属性:主题
AddAttachment()方法:附加文件
Body属性:邮件正文。
下面是一个完整的例子:
public bool sendMail(){MessageClass email = new MessageClass();email.Logging = true;email.Silent = true;email.MailServerUserName = "[email protected]";email.MailServerPassWord = "124";email.From = "[email protected]";
email.Subject = "jmail";email.AddAttachment("c:\\test.xml",true,"");
email.Body = "test jmail send mail";
email.AddRecipient("[email protected]", "abc", null);return email.Send("mail.163.com", false) }
第二种cdonts如:
using System.Web.Mail; //引入名称空间MailMessage ms=new MailMessage();//建立邮件对象
ms.To=email; //string email是对方邮箱地址
ms.From="自己的邮箱地址"; //可以是任意可用smtp地址
ms.Subject="title"; //邮件标题
//----------------------正文--------------------------
ms.Body ="姓名: "+txtname.Text+"\r\n";
ms.Body+="性别: "+txtsex.Text+" 年龄:"+txtage.Text+"\r\n";
ms.Body+="出生地: "+txtaddress.Text+"\r\n";
ms.Body+="出生日期: "+txtbirthday.Text+"\r\n";
//----------------------正文结束------------------------
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//是否需要验证,一般是要的
ms.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
//自己邮箱的用户名
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//自己邮箱的密码 SmtpMail.SmtpServer="smtp.chuguo.org.cn"; //自己邮箱的邮件服务器地址
SmtpMail.Send(ms); //发送
//注:运行该程序时不要开启瑞星的邮件发送监控,否则cpu使用率100%,在vs2003,win2003 下测试通过
ms.To=email; //string email是对方邮箱地址
ms.From="自己的邮箱地址"; //可以是任意可用smtp地址
ms.Subject="title"; //邮件标题
ms.Body=TextBox2.Text; //正文
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//是否需要验证,一般是要的
ms.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
//自己邮箱的用户名
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//自己邮箱的密码 SmtpMail.SmtpServer="smtp.chuguo.org.cn"; //自己邮箱的邮件服务器地址
SmtpMail.Send(ms); //发送
//成功过
'Dim msg As New CDO.Message
'Dim oFields As ADODB.Fields
'Dim config As CDO.IConfiguration
'msg.From = "[email protected]"
'msg.To = "[email protected]"
'msg.Subject = "Test CDO"
'msg.HTMLBody = "<html><body>" + "TEST CDO " + "</body></html>"
'config = msg.Configuration
'oFields = config.Fields
'oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2
'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/languagecode").Value = "0x0804"
'oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = "smtp.163.com"
'oFields.Update()
'msg.BodyPart.Charset = "gb2312"
'msg.HTMLBodyPart.Charset = "gb2312"
'msg.Send()
'msg = Nothing End Sub
mail.To = "[email protected]";
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here SmtpMail.SmtpServer = "mail.mycompany.com"; //your real server goes here
SmtpMail.Send( mail );
{
jmail.Message Jmail=new jmail.Message(); DateTime t=DateTime.Now;
String Subject=" From EMail .net";
String body="你好科学12:15";
String FromEmail="[email protected]";
String ToEmail="[email protected]";
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent=true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging=true;
//字符集,缺省为"US-ASCII"
Jmail.Charset="GB2312";
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
Jmail.ContentType="text/html";
//添加收件人
Jmail.AddRecipient(ToEmail,"","");
Jmail.From=FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName="ljt21" ;
//发件人邮件密码
Jmail.MailServerPassWord="****" ;
//设置邮件标题
Jmail.Subject=Subject;
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
Jmail.AddAttachment( "c:\\test.jpg",true,null);
//邮件内容
Jmail.Body=body+t.ToString();
//Jmail发送的方法
Jmail.Send("smtp.163.com",false);
Jmail.Close() ;
}
ms.To=email; //string email是对方邮箱地址
ms.From="自己的邮箱地址"; //可以是任意可用smtp地址
ms.Subject="title"; //邮件标题
ms.Body=TextBox2.Text; //正文
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//是否需要验证,一般是要的
ms.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
//自己邮箱的用户名
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//自己邮箱的密码 SmtpMail.SmtpServer="smtp.chuguo.org.cn"; //自己邮箱的邮件服务器地址
SmtpMail.Send(ms); //发送
//成功过