2012-06-25 155 views
0

我想用c#发送电子邮件。我在代码中使用了完整的代码,也使用了端口号,主机。但没有收到电子邮件。在asp.net发送电子邮件给yahoo

的.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("[email protected]", "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 
+1

接收?你甚至不知道它是否发送。抓住你不会处理的异常没有意义。它可能在幕后失败,并且你告诉你的程序不要告诉你是否有空的catch块发生。 – Bridge

+0

是的,删除try-catch并告诉我们您收到哪条错误消息。 – Stefan

回答

1

它可能是SSL的故障。 你必须这样启用SSL:

smtpserver.EnableSsl = true; 

希望这可以帮助你。

+0

我这样做,但仍然没有收到.. – user1479485