从本地主机向Gmail发送邮件时出错

时间:2013-04-10 07:25:53

标签: c# asp.net smtpclient

我只是测试从本地主机发送到gmail.com的电子邮件

***Webconfig:***

<system.net>
<mailSettings>
  <smtp>
    <network 
      host="localhost"
       port="25"
        />
  </smtp>
</mailSettings>
</system.net>

***Default.aspx:***

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  Inherits="MasterApps._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />      

</div>
</form>
</body>
</html>

***Code behind is:***

protected void Button1_Click(object sender, EventArgs e)
{

        MailAddress from = new MailAddress("mwaghela7@gmail.com");
        MailAddress to = new MailAddress("mwaghela7@gmail.com");
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = "hi";
        msg.Body = "hello";
        SmtpClient sc = new SmtpClient();
        sc.Send(msg);

}

它被解雇了如下错误:

  

*类型'System.Net.Mail.smtp的例外   在System.dll中发生了failedReceipientExeception,但没有   处理用户代码。附加信息:MailBox不可用   服务器响应是:5.7.1。无法接收mwaghela7@gmail.com

上面的代码有什么问题?怎么解决这个问题?

3 个答案:

答案 0 :(得分:3)

在大多数情况下,由于指定的端口号或未建立安全连接,电子邮件无法发送。试试这个选择。点击here您可以下载一些类文件,以便向所有域发送电子邮件。首先添加对EASendMail的引用。然后是这样的代码。

using EASendMail;
 SmtpMail oMail = new SmtpMail("Tryit");
                    SmtpClient oSmtp = new SmtpClient();

            //        
                    oMail.From = "eamil";

            //        // Set recipient email address
                    oMail.To = "email@domain.com";

                    // Set email subject
                   oMail.Subject = "subject";

                    // Set email body
                    oMail.TextBody = "body";


                    SmtpServer oServer = new SmtpServer("smtp.gmail.com");


                    oServer.User = "email";
                    oServer.Password = "password";



                   oServer.Port = 465;

            //detect SSL type automatically
                 oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
                    oSmtp.SendMail(oServer, oMail);

答案 1 :(得分:0)

您的邮件服务器可能不支持匿名发送邮件。您只需将邮件发送给邮件服务器中的用户。

虽然如下编辑web.config代码,然后尝试:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network host="localhost"
                port="25"
                from="no-reply@me.com"/>
        </smtp>
    </mailSettings>
</system.net>

答案 2 :(得分:0)

试试这个。 `

using (var client = new SmtpClient("smtp.gmail.com", 587))
            {
                client.Credentials = new NetworkCredential("you@gmail.com","password");
               var mail = new MailMessage();
                mail.From = new MailAddress("email");
                mail.To.Add("email");
                mail.Subject = "something";
                mail.Body = "body";
               client.Send(mail);

`