asp.net實現發送郵件(轉自百度知道)__.net

來源:互聯網
上載者:User
private void Page_Load(object sender, System.EventArgs e)
{
string sReturn = string.Empty;
MailMessage mailMsg = new MailMessage();
mailMsg.BodyFormat = MailFormat.Html;
mailMsg.To = "****@163.com";
mailMsg.From = "***@163.com";
mailMsg.Subject = "email.Subject";
mailMsg.Body = "email.Body";

// mailMsg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
// mailMsg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendusername", "使用者名稱");
// mailMsg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendpassword", "密碼");
//
SmtpMail.SmtpServer ="smtp.126.com";//smpt伺服器
try
{

SmtpMail.Send(mailMsg);
this.Label1.Text = "發送成功";
}
catch (Exception err)
{
this.Label2.Text = "<font color=red>發送失敗" + err.Message.ToString() + "</font>";
}


} 回答者: 足球10號 - 千總 四級 5-28 13:57 提問者對於答案的評價: 謝謝給我傳過來的資料 下面是我親手寫的測試發郵件的:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Logic
{
public class Mail
{
public bool ServiceSendMail(string strTo, string strSubject, string strBody, string strEncoding)
{
return common(strTo, "", strSubject, strBody, strEncoding);
}

/// <summary>
/// 郵件發送(Service郵箱),預設編碼為GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strCc"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strEncoding">編碼,如果為空白,預設為GB2312</param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
return common(strTo, strCc, strSubject, strBody, strEncoding);
}

/// <summary>
/// 郵件發送預設編碼為GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strSubject, string strBody)
{
return common(strTo, "", strSubject, strBody, "");
}

private static bool common(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
bool bState = false;

try
{
//編碼暫硬性規定為GB2312
Encoding encoding = Encoding.GetEncoding(936);

MailMessage Message = new MailMessage(
new MailAddress("hanjunhui127@sina.com", "hanjunhui127", encoding),
new MailAddress(strTo));

Message.SubjectEncoding = encoding;
Message.Subject = strSubject;
Message.BodyEncoding = encoding;
Message.Body = strBody;
if (strCc != "")
{
Message.CC.Add(new MailAddress(strCc));
}
SmtpClient smtpClient = new SmtpClient("smtp.sina.com");
smtpClient.Credentials = new NetworkCredential("hanjunhui127","1984127");
smtpClient.Timeout = 999999;
smtpClient.Send(Message);

bState = true;


}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

return bState;
}




}
}

你把上面的代碼編譯成dll,在web程式裡面引用那個dll
在aspx.cs裡面你可以這樣寫
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Logic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Logic.Mail m = new Mail();
m.ServiceSendMail("hjh127@163.com", "你好", "我好啊```````````````");
Response.Write("成功!");
}
}


上面我已經測試成功
如果不行,請繼續參考下面的文章
你可以先參考一下這兩文章:
http://www.5iaspx.com/aspnet/C-XieDeQiChi-SMTP-YanZhengDeFaSongYouJianQuJian-1-peq20134.html
http://www.5iaspx.com/aspnet/C-XieDeQiChi-SMTP-YanZhengDeFaSongYouJianQuJian-2-ci9k0137.html

參考資料:http://www.5iaspx.com 回答者: h1j2h7 - 魔法師 五級 5-20 02:06 麼.......... 回答者: using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using DBModel;

namespace MailSender
{
public class MailUtil
{
#region E-mail發送

public bool SendMail(MailInfo mailInfo)
{
try
{
/*寄件者的資訊*/
string smtp = mailInfo.MailStmp; //發信人所用郵箱的伺服器
string mailForm = mailInfo.MailFrom; //寄件者的郵箱
string mailPwd = mailInfo.MailPwd; //寄件者的密碼
string mailTo = mailInfo.MailTo; //收件者資訊
string mailTitle = mailInfo.MailTitle; //郵件標題
string mailContent = mailInfo.MailContent; //郵件內容

SmtpClient client = new SmtpClient(smtp);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(mailForm, mailPwd);
/*指定如何處理待發的郵件*/
client.DeliveryMethod = SmtpDeliveryMethod.Network;

MailMessage message = new MailMessage(mailForm, mailTo, mailTitle, mailContent);
message.BodyEncoding = Encoding.Default;
message.IsBodyHtml = true;

client.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}

#endregion
}
}



測試過 好用的(MailInfo 是個實體用來存使用者資訊的)
回答者: Imports System.net.Mail
Partial Class mailset
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim client As New SmtpClient("smtp.163.com", 25)
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.Credentials = New System.Net.NetworkCredential("******", "******")
Dim msg As MailMessage
msg = New MailMessage("******@163.com", "******@163.com", "你好", "你好")
msg.IsBodyHtml = True
client.Send(msg)
End Sub
End Class
回答者: 是用Jmail

Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
If SendMail() Then
Label1.Text = "郵件發送成功。"
Label1.CssClass = "red"
End If
End Sub

Function SendMail() As Boolean
Dim smtpServer As String = ConfigurationSettings.AppSettings("smtpServer")
Dim smtpUser As String = ConfigurationSettings.AppSettings("smtpUser")
Dim smtpPass As String = ConfigurationSettings.AppSettings("smtpPass")

Dim result As Boolean
Dim oJmail As New jmail.MessageClass
oJmail.Charset = "gb2312"
oJmail.Encoding = "base64"
oJmail.ContentTransferEncoding = "base64"
'oJmail.ContentType = "text/html"
oJmail.ISOEncodeHeaders = False
oJmail.Priority = Convert.ToByte(3)

oJmail.From = myEmail.Text
oJmail.FromName = Request.Cookies("myCookies")("myName")
oJmail.Subject = Regex.Replace(Title.Text, "<[^>]+>", "")
oJmail.AddRecipient(Email.Text)
oJmail.MailServerUserName = smtpUser
oJmail.MailServerPassWord = smtpPass

'oJmail.Body = Request("Content")
oJmail.AppendHTML(Request("Content").ToString)

'遍曆File表單元素
Dim files As System.web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
Dim iFile As Int32
Try
For iFile = 0 To files.Count - 1
'檢查副檔名字
Dim postedFile As System.Web.HttpPostedFile = files(iFile)
Dim filename As String = postedFile.FileName.ToString
Dim ftype As String = postedFile.ContentType.ToString
Dim filesize As Integer = postedFile.ContentLength
Dim sOriginalFileName = System.IO.Path.GetFileName(filename)
Dim sFileExt As String = System.IO.Path.GetExtension(sOriginalFileName)
If filename <> "" Then
oJmail.AddAttachment(filename, False, filename.Substring(filename.LastIndexOf(".") + 1, 3))
End If
Next
Catch Ex As System.Exception
Label1.CssClass = "red"
Label1.Text = Ex.Message
Return False
End Try

If oJmail.Send(smtpServer, False) Then
result = True
Else
result = False
End If
oJmail.Close()
oJmail = Nothing
Return result
End Function
回答者: 回答者: 海洋X - 資深經理 七級 5-26 23:00 '參數title是標題,Boby內容,TOWHO要發送給的郵件地址,發送成功返回空,發送失敗返回錯誤訊息
VB.NET做法
Public Function SendMail(ByVal title As String, ByVal Body As String, ByVal ToWho As String) As String
Dim Msg As New MailMessage
Msg.From = "你的郵件地址"
Msg.To = ToWho
Msg.Subject = title
Msg.BodyFormat = MailFormat.Html
Msg.BodyEncoding = System.Text.Encoding.Default
Msg.Priority = MailPriority.High
Msg.Body = Body
Msg.Fields.Add("
  http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
Msg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendusername", "你的郵箱登入名稱稱")
Msg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendpassword", "你的郵箱登入地址")
Try
System.Web.Mail.SmtpMail.SmtpServer = "郵件伺服器地址,如smtp.126.com"
System.Web.Mail.SmtpMail.Send(Msg)
SendMail = ""
Catch ex As Exception
SendMail = ex.Message
End Try
End Function
c#做法
public string SendMail(string title,string body,string towho)
{
MailMessage msg=new MailMessage();
Msg.From = "你的郵件地址";
Msg.To = ToWho;
Msg.Subject = title;
Msg.BodyFormat = MailFormat.Html;
Msg.BodyEncoding = System.Text.Encoding.Default;
Msg.Priority = MailPriority.High;
Msg.Body = Body;
Msg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Msg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendusername", "你的郵箱登入名稱稱");
Msg.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendpassword", "你的郵箱登入地址");
try
{
System.Web.Mail.SmtpMail.SmtpServer = "郵件伺服器地址,如smtp.126.com"
System.Web.Mail.SmtpMail.Send(Msg)
return "";
}
catch(Exception exp)
{
return ex.Message;
}

} http://www.51aspx.com/CV/JH1GEZ9ACZA70/

可以發送郵件,選擇是否html格式等 liujiayu10 - 江湖新秀 四級 5-26 08:49 xuyue3000 - 見習魔法師 二級 5-25 19:27 soar_gh - 試用期 一級 5-25 10:42 sdn110 - 助理 二級 5-20 10:23
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.