|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我有个同学,他是搞Java的,他给我说“Java不是效率低,而是速度慢。”,我不是搞Java的,我实在想不透这句话的含义,难道执行速度不就是效率低吗?难道执行速度慢还成效率高了?发送邮件|技能用System.Web.Mail发送邮件,合用于.net1.1,.net2.0请用System.Net.Mail
先援用System.Web
1,发送复杂邮件
[C#]
MailMessagemail=newMailMessage();mail.To="me@mycompany.com";mail.From="you@yourcompany.com";mail.Subject="thisisatestemail.";mail.Body="thisismytestemailbody";SmtpMail.SmtpServer="localhost";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]
DimmailAsNewMailMessage()mail.To="me@mycompany.com"mail.From="you@yourcompany.com"mail.Subject="thisisatestemail."mail.Body="thisismytestemailbody"SmtpMail.SmtpServer="localhost"yourrealservergoeshereSmtpMail.Send(mail)这里的smtpserver只能是那些不必要考证的smtp服务器,像126,sina,yahoo等等的邮箱,都必要考证,以是不克不及用。用这些邮箱发信前面会讲到2,发送Html邮件[C#]MailMessagemail=newMailMessage();mail.To="me@mycompany.com";mail.From="you@yourcompany.com";mail.Subject="thisisatestemail.";mail.BodyFormat=MailFormat.Html;mail.Body="thisismytestemailbody.<br><b>thispartisinbold</b>";SmtpMail.SmtpServer="localhost";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]DimmailAsNewMailMessage()mail.To="me@mycompany.com"mail.From="you@yourcompany.com"mail.Subject="thisisatestemail."mail.BodyFormat=MailFormat.Htmlmail.Body="thisismytestemailbody.<br><b>thispartisinbold</b>"SmtpMail.SmtpServer="localhost"yourrealservergoeshereSmtpMail.Send(mail)
3,发送附件
[C#]
MailMessagemail=newMailMessage();mail.To="me@mycompany.com";mail.From="you@yourcompany.com";mail.Subject="thisisatestemail.";mail.Body="thisismytestemailbody.";MailAttachmentattachment=newMailAttachment(Server.MapPath("test.txt"));//createtheattachmentmail.Attachments.Add(attachment);//addtheattachmentSmtpMail.SmtpServer="localhost";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]
DimmailAsNewMailMessage()mail.To="me@mycompany.com"mail.From="you@yourcompany.com"mail.Subject="thisisatestemail."mail.Body="thisismytestemailbody."DimattachmentAsNewMailAttachment(Server.MapPath("test.txt"))createtheattachmentmail.Attachments.Add(attachment)addtheattachmentSmtpMail.SmtpServer="localhost"yourrealservergoeshereSmtpMail.Send(mail)
4,修正发件人和收件人的称号好比发件人的地点是abc@126.com,我们用outlook收到信,From一栏里将间接显现abc@126.com.能不克不及在From一栏里显现友爱一点的名字呢?好比显现TonyGong办法以下:[C#]MailMessagemail=newMailMessage();mail.To=""John"<";mail.From>me@mycompany.com>";mail.From=""TonyGong"<";mail.Subject>you@yourcompany.com>";mail.Subject="thisisatestemail.";mail.Body="thisismytestemailbody.";SmtpMail.SmtpServer="localhost";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]DimmailAsNewMailMessage()mail.To="""John""<"mail.From>me@mycompany.com>"mail.From="""TonyGong""<"mail.Subject>you@yourcompany.com>"mail.Subject="thisisatestemail."mail.Body="thisismytestemailbody."SmtpMail.SmtpServer="localhost"yourrealservergoeshereSmtpMail.Send(mail)
5,发送给多人
[C#]
MailMessagemail=newMailMessage();mail.To="me@mycompany.com;him@hiscompany.com;her@hercompany.com";mail.From="you@yourcompany.com";mail.Subject="thisisatestemail.";mail.Body="thisismytestemailbody.";SmtpMail.SmtpServer="localhost";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]
DimmailAsNewMailMessage()mail.To="me@mycompany.com;him@hiscompany.com;her@hercompany.com"mail.From="you@yourcompany.com"mail.Subject="thisisatestemail."mail.Body="thisismytestemailbody."SmtpMail.SmtpServer="localhost"yourrealservergoeshereSmtpMail.Send(mail)
6,用必要Smtp考证的邮箱发信
如今为了避免渣滓邮件,尽年夜部分Smtp服务器必要考证了
发信办法以下:
[C#]
MailMessagemail=newMailMessage();mail.To="me@mycompany.com";mail.From="abc@126.com";mail.Subject="thisisatestemail.";mail.Body="Sometextgoeshere";mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");//basicauthenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","abc");//setyourusernameheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","yourpassword");//setyourpasswordhereSmtpMail.SmtpServer="smtp.126.com";//yourrealservergoeshereSmtpMail.Send(mail);
[VB.NET]
DimmailAsNewMailMessage()mail.To="me@mycompany.com"mail.From="abc@126.com"mail.Subject="thisisatestemail."mail.Body="Sometextgoeshere"mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1")basicauthenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","abc")setyourusernameheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","YourPassword")setyourpasswordhereSmtpMail.SmtpServer="smtp.126.com"yourrealservergoeshereSmtpMail.Send(mail)7,修正smtp服务器的端口,和利用SSL加密年夜部分smtp服务器的端口是25,但有些却不是同时,尽年夜部分Smtp服务器不必要SSL上岸,有些却必要好比Gmail,smtp端口是:465,同时撑持SSL代码以下:[C#]MailMessagemail=newMailMessage();mail.To="me@mycompany.com";mail.From="abc@126.com";mail.Subject="thisisatestemail.";mail.Body="Sometextgoeshere";mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");//basicauthenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","abc");//setyourusernameheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","yourpassword");//setyourpasswordheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465);mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl","true");
SmtpMail.SmtpServer="smtp.126.com";//yourrealservergoeshereSmtpMail.Send(mail);[VB.NET]DimmailAsNewMailMessage()mail.To="me@mycompany.com"mail.From="abc@126.com"mail.Subject="thisisatestemail."mail.Body="Sometextgoeshere"mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1")basicauthenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","abc")setyourusernameheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","YourPassword")setyourpasswordheremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465)mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl","true")SmtpMail.SmtpServer="smtp.126.com"yourrealservergoeshereSmtpMail.Send(mail)
我实在想不明白java的机制,为什么非要那么蛋疼,在同一个平台下重复编译。 |
|