0

我正在尝试使用 SmtpMail 发送电子邮件。在我的本地服务器上,即使代码具有主机/服务器、密码和代码中设置的信息,我也可以执行此操作。在我的托管服务 (1and1) 上,它只会在我在 web.config 文件中设置这些内容时发送!有谁知道可能导致这种情况的原因(1and1 不知道)。

在本地工作,但不在托管服务上

<%@ Page language="c#" %>
<%@ Import namespace="System.Net.Mail" %>
<%

    MailMessage mail = new MailMessage();

    //Set my from address
    mail.From = new MailAddress( "myemail@myemail.com");

    //Who I'm sending to
    mail.To.Add( new MailAddress("you@you.com") );

    mail.Subject = "A test";
    mail.Body = "Test message";

    //Set the mail server (default should be smtp.1and1.com)
    SmtpClient smtp = new SmtpClient( "smtp.1and1.com" );

    //Enter your full e-mail address and password
    smtp.Credentials = new NetworkCredential("myemail@myemail.com", "mypassword");

    //Send the message
    smtp.Send(mail);

%>

这就是我如何让它在主机上工作(两个文件,测试页和 web.config)

<%@ Page language="c#" %>
<%@ Import namespace="System.Net.Mail" %>
<%

    MailMessage mail = new MailMessage();

    //Set my from address
    mail.From = new MailAddress( "myemail@myemail.com");

    //Who I'm sending to
    mail.To.Add( new MailAddress("you@you.com") );

    mail.Subject = "A test";
    mail.Body = "Test message";

    //Create with no server or credentials (grabs from web.config)
    SmtpClient smtp = new SmtpClient();

    //Send the message
    smtp.Send(mail);

%>

(和 web.config)

    <configuration>
    <system.net>
        <mailSettings>
            <smtp from="myemail@myemail.com">
                <network host="smtp.1and1.com" port="25" userName="myemail@myemail.com" password="mypassword"/>
            </smtp>
        </mailSettings>
    </system.net>
</configuration>
4

1 回答 1

1

我看到您在使用本地代码时没有设置 SMTP 端口,而是在 web.config 中设置了它。您是否尝试过像这样手动添加 SMTP 端口:

smtp.Port = 25;

另外,您是否尝试过手动设置 EnableSsL 属性?

于 2013-02-04T18:43:40.887 回答