【问题标题】:sending email in c# using port no在 C# 中使用端口号发送电子邮件
【发布时间】:2012-07-01 00:11:51
【问题描述】:

我想从 yahoo 向 yahoo 发送电子邮件,但发送失败。我用网络收到。请查看我的代码凭据。没有异常或错误,但是当打开邮箱时,没有电子邮件。我使用了一个label,它显示了成功的全文,但当时并没有显示成功。

.aspx 代码:

<form id="form1" runat="server">

    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>
</form>

.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("username", "password");

         //port number to login yahoo server
         SmtpServer.Port = 587;

         //yahoo host name
         SmtpServer.Host = "smtp.mail.yahoo.com";
         SmtpServer.EnableSsl = true;

         SmtpServer.Send(mM);
         Label1.Text = "successfull";
         //Send the email

         //end of try block
         //end of catch
     }
     catch(Exception ee){
         Console.Write(ee);
     }
}

【问题讨论】:

  • 立即更改您的密码。
  • 您正在捕获异常并将其打印到控制台。你能告诉我们正在打印的错误吗?还是没有错误?

标签: c# asp.net .net email


【解决方案1】:

我不认为代码本身有问题。我几乎使用与您相同的代码,并且效果很好。

我认为你的设置有问题,请检查:

  1. 如果您的 TextBox1 中收件人的电子邮件地址已经存在
  2. 如果凭据的用户名和密码都正确

而且我没有在我的代码中设置 Port 属性。如果一切正常。请在此处显示您的异常信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 2013-08-28
    • 2017-02-17
    • 2012-02-25
    • 2010-10-01
    相关资源
    最近更新 更多