问题
we use the SMTP ports 587 for sending emails through gmail associated account in C# ASP.NET. So how can we apply a port/host to send emails from any account like yahoo,outlook etc?
回答1:
You can achieve what you want by:
Identify if the mail services expose SMTP ports for public use
Programatically switch the host/port according to the service the code is talking to.
Something like
If the email address is yahoo, then connect to yahoo SMTP server.
回答2:
You should keep the SMTP information in web.config (read about how to do this).
Then in you code do something like 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);
If you wrap this code into a method (say, MyOwnMailSender.Send()
) you can simplify it further, for example you can then invoke:
MyOwnMailSender.Send(addresses, title, body);
回答3:
Below is the code for setting the host and port sections of a new SMTP Object
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");
Then you could call it in a switch statement
switch(account){
case "yahoo" :
sendmailYahoo();
break;
case "hotmail" :
sendmailHotmail();
break;
}
obviously with this case you would have seperate methods or you could call it during the method so you could have
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;
}
回答4:
If you are using SmtpClient, you can configure the port in the web.config file:
<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>
See MSDN
回答5:
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);
}
}
回答6:
This is the Working Code of SMTP Email Sender
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;
}
}
来源:https://stackoverflow.com/questions/21756002/email-sending-in-c-sharp-asp-net-query-smtp-port