Asp.net 邮件传输(转)
下面說下我總結辦法:
第一種方法:
using System.Web.Mail;
public void sendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here";
mail1.From="xxx@xxx.com";
mail1.To="yyy@yyy.com";
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx@xxx.com");
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********");
SmtpMail.SmtpServer="mail.xxx.com";
SmtpMail.Send(mail1);
}
以上添加的幾個 Fields 是用來作SMTP發信認證的,如果你的發信服務器不需要認證,就可以省略這幾句。
第二種方法:
using System.Net.Mail;
方法一:向單個地址發送郵件,不設置web.config文件
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password");
SmtpClient smtp = new SmtpClient("smtp.company.com");
smtp.Credentials = credential;
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
方法二、向單個地址發送郵件,設置web.config文件
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
方法三:群發郵件,設置web.config文件
public void SendEmail()
{
string mailto = "to1@company.com,to2@company.com";
string title = "mail title here";
string content = "mail content here";
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
MailAddressCollection address = new MailAddressCollection();
string[] mailtos = mailto.Split(',');
for (int i = 0; i < mailtos.Length; i++)
{
address.Add(mailtos[i]);
}
foreach (MailAddress add in address)
{
message.To.Add(add);
}
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = title;
message.Body = content;
smtp.Send(message);
message.Dispose();
address.Clear();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
采用以上方法,如果運行發信程序的計算機上裝有郵件監控等殺毒軟件,會有失敗的警告,但實際已發送成功。解決辦法是關閉殺毒軟件的監控功能。
下面這個記得加上命名空間:
using System.Net;
using System.Net.Cache;
using System.Net.Mail;
using System.Net.Configuration;
using System.Web.Configuration;
適用的框架:asp.net framework 2.0/.net framework3.0/.net framework3.5
在web.config里設置.net framework的網絡連接
?
XML/HTML代碼如下:設置stmp郵件發送的配置
?
XML/HTML代碼?
以編程的方式獲取web.config里的smtp配置
NetSectionGroup類
命名空間
System.Net.Configuration:為應用程序提供了以編程方式訪問和更新配置文件System.Net命名空間下的設置的類。
程序集
System
定義:
public sealed class NetSectionGroup : ConfigurationSectionGroup
說明:
這個類提供了以程序方式訪問存儲在配置文件里的信息。
這個類和網絡設置文檔里的system.net元素(網絡設置)相對應。
這部分的為下列名稱空間提供配置設置:
System.Net
System.Net.Cache
System.Net.Mail
在程序里讀取configuration/system.net/mailSettings/stmp配置
?
C#代碼?
發送電子郵件
C#代碼[轉]:http://www.cnblogs.com/matrix/archive/2004/05/20/10495.aspx
?
現在的郵件發送大多數需要STMP的身份驗證,
.NET里面的
System.Web.Util
System.Web.Mail
就不可以了.
我寫這篇文章是希望對大家開發項目有所幫助,高手見笑了
先要去找一個組件,名字叫 JMail
大家可以去網上找一下,下載下來安裝上.
將jmail.dll引用到工程中
?
private void Button1_Click(object sender, System.EventArgs e)
{
???
?? jmail.Message Jmail=new jmail.Message();
?? DateTime t=DateTime.Now;??
?? String Subject=" From EMail .net";??
?? String body="你好科學12:15";
?? String FromEmail="ljt21@163.com";??
?? String ToEmail="xiao-maolover@163.com";
?? //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() ;
}
這樣就OK了..!
經過測試的,沒有問題.
Asp.net 自動發送郵件的方法
今天有一個模塊需要自動發送郵件的功能,就隨便寫了一個,記錄一下作為積累。
一、首先需要配置web.config文件:
<system.net>
<mailSettings>
??? <smtp from="Emailname">
??????? <network host="smtp.163.com" userName="Emailname" password="Emailpassword"
??????? port="25" defaultCredentials="false"/>
??? </smtp>
</mailSettings>
</system.net>
二、然后編寫發送郵件的函數:
<summary>
/// 郵件發送方法(帶附件)
/// </summary>
/// <param name="mailto">收件人地址。如:receiver@163.com</param>
/// <param name="mailsubject">郵件標題</param>
/// <param name="mailbody">郵件正文</param>
/// <param name="mailFrom">郵件發送人地址。如:sender@163.com</param>
/// <param name="list">附件路徑</param>
/// <returns></returns>
public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
{
try
{
??? //郵件發送人地址
??? System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
??? //如test@163.com,初步測試,用test@sina.com不行,用163的郵件服務器,就必須用163郵箱的用戶名
??? //收件人地址
??? System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
??? System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
??? mail.Subject = mailsubject;
??? mail.Body = mailbody;
??? //以下設置服務器
??? System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
??? //以下為增加附件
??? int count = list.Count;
??? for (int i = 0; i < count; i++)
??? {
????? System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
????? mail.Attachments.Add(data);
??? }
??? mySmth.Send(mail);
??? mail.Dispose();
??? return true;
}
catch
{
??? return false;
}
}
三、最后就是對函數的調用了:
//自動發送郵件
string mailSubject = "會員注冊確認函";
string mailBody = "正文內容。";
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
ArrayList List = new ArrayList();
List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
{
...
//發送成功,進行相應處理
}
else
{
...
//發送失敗,進行相應處理
return;
}
轉載于:https://www.cnblogs.com/xiachufeng/archive/2010/05/18/1738210.html
總結
以上是生活随笔為你收集整理的Asp.net 邮件传输(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转载:QT图形视图框架(The Grap
- 下一篇: Apache认证、授权和访问控制