当前位置: 代码迷 >> ASP.NET >> 在asp.net中发送邮件解决方法
  详细解决方案

在asp.net中发送邮件解决方法

热度:9520   发布时间:2013-02-25 00:00:00.0
在asp.net中发送邮件
//命名空间: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"的账号就不行了,这是什么原因了。。。


------解决方案--------------------------------------------------------
http://www.cnblogs.com/hymxtang/archive/2007/06/27/797247
------解决方案--------------------------------------------------------
From应该是个电子邮件地址
例如china_cor@google.com
------解决方案--------------------------------------------------------
用邮件服务器ip地址的话,应该还要设置端口了吧,你要知道它把域名映射到了哪个端口上。
NET环境下Email的技术介绍
------解决方案--------------------------------------------------------
指定字符串与电子邮件地址所要求的形式不符

一妹地址不符合要求

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.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);        }    }}
------解决方案--------------------------------------------------------
C# code
/// <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                {                    //                }            }        }    }
  相关解决方案