日期:2014-05-17  浏览次数:20454 次

SmtpClient发邮件 无法连接到远程服务器
MailMessage mailMessage = new MailMessage();
  mailMessage.To.Add("123456789@qq.com");
  mailMessage.From = new System.Net.Mail.MailAddress("xxxxxx@163.com");
  mailMessage.Subject = "Tttt";
  mailMessage.Body = "jkkkkkkkkkkkkkkkkkkkkkkkkkk";
  mailMessage.IsBodyHtml = true;
  mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
  string password = "mima";
  SmtpClient smtpClient = new SmtpClient();
  smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password); 
  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  smtpClient.Host = "smtp." + mailMessage.From.Host;
  smtpClient.Send(mailMessage);
上面的代码,本机测试总是不行,错误提示 无法连接到远程服务器,在其他同事电脑上,测试通过,能收到邮件。晕啊...大虾们给个说法

------解决方案--------------------
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);
        }
    }
}