在asp.net中发送邮件

qq394467044 2011-07-15 12:10:58
//命名空间:using System.Net.Mail;
using System.Net.Mime;

//方法:
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string
strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;

System.Net.Mail.MailMessage message =
new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
调用:SendSMTPEMail("10.3.0.17", "china_cor", "*****","mrchuei@163.com", "测试中。。", msgBody.ToString());

报错提示:指定字符串与电子邮件地址所要求的形式不符

求高手指教,这个问题困扰我很久了,我用matp.163.com的服务器发邮件可以,但是用"10.3.0.17"和账号为"chin_cor"的账号就不行了,这是什么原因了。。。
...全文
496 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwssdd2012 2012-06-11
  • 打赏
  • 举报
回复
<system.net>
<mailSettings>
<smtp from="wwssdd2000@163.com">
<network host="113.108.225.9" password="zxc6626632" userName="wwssdd2000" />
</smtp>
</mailSettings>

</system.net>

在webconfig加入
wangxiaofeiwuqiao 2011-07-28
  • 打赏
  • 举报
回复

public void sendEmail(string sender, string senderuser, string euser, string epwd, string receiver, string subject, string body, string eserver)
{
jmail.MessageClass jmMessage = new jmail.MessageClass();
jmMessage.Charset = "GB2312";
jmMessage.ISOEncodeHeaders = false;
jmMessage.From = sender;
jmMessage.FromName = senderuser;
jmMessage.Subject = subject;
jmMessage.MailServerUserName = euser;
jmMessage.MailServerPassWord = epwd;
jmMessage.AddRecipient(receiver, "", "");
// if (this.upFile.PostedFile.ContentLength != 0)
//{
// string sFilePath = this.upFile.PostedFile.FileName;
// jmMessage.AddAttachment(@sFilePath, true, "");
// }
jmMessage.Body = body;
if (jmMessage.Send(eserver, false))
{
// Response.Write("<script language=javascript>alert('发送成功')</script>");
}
else
{
Response.Write("<script language=javascript>alert('发送失败,请仔细检查邮件服务器的设置是否正确!')</script>");
jmMessage = null;
return;
}
}
jacklyne 2011-07-22
  • 打赏
  • 举报
回复
关注一下,我们也要用..
fanxin_1999 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxr0323 的回复:]
指定字符串与电子邮件地址所要求的形式不符

一妹地址不符合要求


C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S……
[/Quote]

刚刚做好一个,是参考wxr0323 做的,百分百能运行。如果需要,我可以发给你,
yonghot 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxr0323 的回复:]

指定字符串与电子邮件地址所要求的形式不符

一妹地址不符合要求

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.W……
[/Quote]


谢谢wxr0323 ,这个对我非常有帮助。。
子夜__ 2011-07-15
  • 打赏
  • 举报
回复
指定字符串与电子邮件地址所要求的形式不符

一妹地址不符合要求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;

namespace MailSender
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment;

// 创建一个附件对象
objMailAttachment = new MailAttachment("C:\\1.xml");//发送邮件的附件

// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "mytest110@sina.com";//源邮件地址
objMailMessage.To = "********@qq.com";//目的邮件地址
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中

//接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");

//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "mytest110");

//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******");

//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为:530 Authentication required

//SMTP地址
SmtpMail.SmtpServer = "smtp.sina.com";

// 开始发送邮件
// 在发送之前,去新浪邮箱里开启POP/SMTP设置 邮箱设置->账户->POP/SMTP设置->开启
// 否则会报错误0x80040217. The server response was not available
SmtpMail.Send(objMailMessage);
}
}
}
dalmeeme 2011-07-15
  • 打赏
  • 举报
回复
用邮件服务器ip地址的话,应该还要设置端口了吧,你要知道它把域名映射到了哪个端口上。
NET环境下Email的技术介绍
宝_爸 2011-07-15
  • 打赏
  • 举报
回复
From应该是个电子邮件地址
例如china_cor@google.com
zhmjppp 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxr0323 的回复:]

指定字符串与电子邮件地址所要求的形式不符

一妹地址不符合要求

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.W……
[/Quote]


正解。。。。
ZHUKY 2011-07-15
  • 打赏
  • 举报
回复

/// <summary>
/// 邮件发送
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSendEmail_Click(object sender, EventArgs e)
{
string _x_ccono = X_CCONo.Text.Trim();
if (!_x_ccono.Equals(string.Empty))
{
XJY_YS.BLL.CF.C_Complaints complaintsBll = new XJY_YS.BLL.CF.C_Complaints();
XJY_YS.Model.CF.C_Complaints complaintsModel = complaintsBll.GetModel(_x_ccono);
if (complaintsModel != null)
{
CreateExcel(complaintsModel);//创建Excel
GC.Collect(); //Kill进程

//主要是这段邮件内容调用
try
{
string from = FromName;
string password = FromPass;
string to = ToName;
//取出用户名
int nameLength = from.IndexOf('@');
string username = from.Substring(0, nameLength);

string subject = "";//主题
string body = "";//内容
string server = "smtp.gmail.com";//服务器地址 SMTP (这里是谷歌邮箱的)
string fileEndName = "文件.xls";
string fileUpLoad = Server.MapPath("~/EmailSend/" + fileEndName);

SendEmail(username, password, server, from, to, subject, body, fileUpLoad);
}
catch
{
//
}
}
}
}


/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="username">发送邮件方的登陆用户名(注:这里并不是邮箱名称,而是邮件@前的名称)</param>
/// <param name="pwd">登陆密码</param>
/// <param name="server">SMTP传输协议服务器地址(发送邮件方的服务器地址)</param>
/// <param name="fromemail">发送邮件方的邮箱账号</param>
/// <param name="tomail">接收邮件方的邮箱账号</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文内容</param>
/// <param name="fileUpLoad">邮件附件(上传到服务器的Excel文件)</param>
/// <returns>成功返回 True / 失败返回 False</returns>
private bool SendEmail(string username, string pwd, string server, string fromemail, string tomail, string subject, string body, string fileUpLoad)
{
bool isFlag = true;
try
{
//创建传输协议smtp对象
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

//创建发送邮件类对象
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//发送邮件方
mail.From = new System.Net.Mail.MailAddress(fromemail);
//接收邮件方
mail.To.Add(new System.Net.Mail.MailAddress(tomail));
//附件文件
mail.Attachments.Add(new Attachment(fileUpLoad));
//邮件主题
mail.Subject = subject;
//正文内容
mail.Body = body;
//是否Html格式
mail.IsBodyHtml = true;
//SMTP服务器地址
smtp.Host = server;
//发送连接时间
smtp.Timeout = int.MaxValue;
smtp.UseDefaultCredentials = false;
//发送端口
smtp.Port = 587;
//是否加密
smtp.EnableSsl = true;
//发送邮件方的身份验证(登陆名称/密码)
smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
//发送邮件对象
smtp.Send(mail);
}
catch
{
isFlag = false;
}
return isFlag;
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧