0

我想使用 c# 发送电子邮件。我实现了整个代码,也在代码中使用端口号、主机。但未收到电子邮件。

.aspx

Message from: <asp:TextBox ID="text1" runat="server"></asp:TextBox>
Message To: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Message subject: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="b1" runat="server" OnClick="click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

.aspx.cs

public void click(object sender, EventArgs e)
{
    try
    {
        //mail message
        MailMessage mM = new MailMessage();
        //Mail Address
        mM.From = new MailAddress( text1.Text);
        //emailid to send
        mM.To.Add(TextBox1.Text);
        //your subject line of the message
        mM.Subject = "your subject line will go here.";
        //now attached the file
        //mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
        //add the body of the email
        mM.Body = "Your Body of the email.";
        mM.IsBodyHtml = false;
        //SMTP 
        SmtpClient SmtpServer = new SmtpClient();
        //your credential will go here
        SmtpServer.Credentials = new System.Net.NetworkCredential("sender@yahoo.com", "password");
        //port number to login yahoo server
        SmtpServer.Port = 587;
        //yahoo host name
        SmtpServer.Host = "smtp.mail.yahoo.com";
        SmtpServer.Send(mM);
        Label1.Text = "successfull";
        //Send the email

    }//end of try block
    catch (Exception ex)
    {
    }//end of catch
}//end of Yahoo Email Method
4

1 回答 1

1

这可能是 SSL 的错误。您必须像这样启用 SSL:

smtpserver.EnableSsl = true;

希望这对您有所帮助。

于 2012-06-25T15:22:49.723 回答