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

本文关键字:出错 Gmail 主机 | 更新日期: 2023-09-27 18:08:28

我正在测试从本地主机发送到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,但不是在用户代码中处理。附加信息:邮箱不可用。的服务器响应为:5.7.1。无法中继mwaghela7@gmail.com

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

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

在大多数情况下,由于指定的端口号或您没有建立安全连接,电子邮件发送失败。试试这个选择。点击这里你可以下载一些类文件发送电子邮件到所有的域。首先添加对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);

您的邮件服务器可能不支持匿名发送邮件。你只能把邮件发送给在你的邮件服务器上的用户。

虽然编辑你的网页。配置如下代码,然后尝试:

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

试试这个。'

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);

"