An open API service indexing awesome lists of open source software.

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#

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;
}
}

```