在C#ASP.NET查询SMTP PORT中发送电子邮件

时间:2014-02-13 13:44:31

标签: c# asp.net email smtp

我们使用SMTP端口587通过C#ASP.NET中的gmail关联帐户发送电子邮件。那么我们如何应用端口/主机从任何帐户发送电子邮件,如雅虎,Outlook等?

6 个答案:

答案 0 :(得分:0)

您可以通过以下方式实现目标:

  1. 确定邮件服务是否公开SMTP端口供公众使用

  2. 根据代码正在与之交谈的服务以编程方式切换主机/端口。

  3. 这样的东西

    如果电子邮件地址是yahoo,则连接到yahoo SMTP服务器。

答案 1 :(得分:0)

您应该将SMTP信息保存在web.config (read about how to do this)中。

然后在你的代码中执行以下操作:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("admin@google.com");
message.Subject = "Hello";
message.From = new System.Net.Mail.MailAddress("admin@google.com");
message.Body = "Hello world.";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("host_name_defined_in_web_config");
smtp.Send(message);

如果将此代码包装到方法中(例如,MyOwnMailSender.Send()),您可以进一步简化它,例如,您可以调用:

MyOwnMailSender.Send(addresses, title, body);

答案 2 :(得分:0)

以下是设置新SMTP对象的主机和端口部分的代码

SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("test", "testpass");
smtp.Host = "mail.test.co.uk";
smtp.Port = Convert.ToInt32("22");

然后你可以在switch语句中调用它

switch(account){
      case "yahoo" :
      sendmailYahoo();
      break;
      case  "hotmail" :
      sendmailHotmail();
      break;
}

显然在这种情况下你会有单独的方法,或者你可以在方法中调用它,所以你可以有

SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

switch(account){
      case "yahoo" :
      smtp.Credentials = new NetworkCredential("yahoo", "testpass");
      smtp.Host = "mail.yeahoo.co.uk";
      smtp.Port = Convert.ToInt32("212");
      break;
      case  "hotmail" :
       smtp.Credentials = new NetworkCredential("hotmail", "testpass");
       smtp.Host = "mail.hotmail.co.uk";
       smtp.Port = Convert.ToInt32("232");
      break;
}

答案 3 :(得分:0)

如果您使用的是SmtpClient,则可以在web.config文件中配置端口:

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network
          clientDomain="www.contoso.com"
          defaultCredentials="true"
          enableSsl="false"
          host="mail.contoso.com"
          port="25"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

请参阅MSDN

答案 4 :(得分:0)

       public void MailSend()
        {
            using (MailMessage mailMessage = new MailMessage())
            {
                mailMessage.From = new MailAddress("mail@gmail.com");
                mailMessage.Subject = "subject";
                mailMessage.Body = "body";
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(mail));


                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = mailMessage.From.Address;
                NetworkCred.Password = "password";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mailMessage);
            }
        }

答案 5 :(得分:0)

这是SMTP电子邮件发件人的工作代码

  public static bool SendEmail(string attachment)
    {
        try
        {
            string smtp = "smtp.gmail.com";
            int port = 587;
            string from = "Gmail Account";
            string password = "Account Password";
            string DisplayName = "Email Subject";
            bool IsHTML = false;
            bool IsSSLEnable = true;
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smtp);
            mail.From = new MailAddress(from, DisplayName);
            mail.To.Add("Reciever Email");
            mail.Subject = DisplayName ;
            mail.IsBodyHtml = IsHTML;
            mail.Attachments.Add(new Attachment(attachment));
            mail.Body = "This is auto Generated Report";
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(from, password);
            SmtpServer.EnableSsl = IsSSLEnable;
            SmtpServer.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return false;
        }
    }