https://github.com/anicetkeric/websendemail
send an Email using C#
https://github.com/anicetkeric/websendemail
asp-net csharp-code smtp
Last synced: 6 months ago
JSON representation
send an Email using C#
- Host: GitHub
- URL: https://github.com/anicetkeric/websendemail
- Owner: anicetkeric
- Created: 2017-04-18T10:42:14.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-07T22:20:55.000Z (about 9 years ago)
- Last Synced: 2024-12-29T08:11:46.099Z (over 1 year ago)
- Topics: asp-net, csharp-code, smtp
- Language: C#
- Size: 8.17 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WebSendEmail
send an Email using C#
## Server Parameters
| Server Name | SMTP Address |Port | SSL
| ------------- |:-------------:| -----|-----|
| Yahoo! | smtp.mail.yahoo.com |587 |Yes |
|Gmail | smtp.gmail.com | 587| Yes
| Hotmail| smtp.live.com | 587 | Yes
| Outlook|smtp-mail.outlook.com | 587 | Yes |
## main class
```c#
public class Email
{
public string toList { get; set; }
public string ccList { get; set; }
public string subject { get; set; }
public string body { get; set; }
}
public class GmailSmtp
{
public static int portNumber = 587;
public static string smtpAddress = "smtp.gmail.com";
public static bool enableSSL = true;
public static string sender = "";
public static string password = "";
}
public class YahooSmtp
{
public static int portNumber = 587;
public static string smtpAddress = "smtp.mail.yahoo.com";
public static bool enableSSL = true;
public static string sender = "";
public static string password = "";
}
public class HotmailSmtp
{
public static int portNumber = 587;
public static string smtpAddress = "smtp.live.com";
public static bool enableSSL = true;
public static string sender = "";
public static string password = "";
}
public class OutlookSmtp
{
public static int portNumber = 587;
public static string smtpAddress = "smtp-mail.outlook.com";
public static bool enableSSL = true;
public static string sender = "";
public static string password = "";
}
}
```
## SendMail method with gmail smtp
This method use gmail.smtp
```c#
public static string SendMail(Email email)
{
string from = GmailSmtp.sender;
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(email.toList);
if (email.ccList != null && email.ccList != string.Empty)
message.CC.Add(email.ccList);
message.Subject = email.subject;
message.IsBodyHtml = true;
message.Body = String.Format("
{0}", email.body);
smtpClient.Host = GmailSmtp.smtpAddress; // We use gmail as our smtp client
smtpClient.Port = GmailSmtp.portNumber;
smtpClient.EnableSsl = GmailSmtp.enableSSL;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(GmailSmtp.sender, GmailSmtp.password);
smtpClient.Send(message);
msg = "1";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
}
```