asp.net实现发送邮件

来源:互联网 发布:单例模式java代码 编辑:程序博客网 时间:2024/06/02 22:46

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/

该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/200771414914.Html

原创粉丝点击