[C#] Mail 보내기 - Gmail
public class CSmtp
{
/// <summary>
/// multiple recipients 일때 "," 로 여러개 사용 가능 ("xxx.yyy@yyy.com,xxx.yyy@yyy.com,xxx.yyy@yyy.com")
/// </summary>
/// <param name="toAddress"></param>
/// <param name="filePaths"></param>
public static void SendEmail(string toAddress, List<string> filePaths, string emailSubject)
{
SmtpClient smtp = null;
MailMessage message = null;
Attachment attachment = null;
string fromAddress = "@gmail.com";
string fromPassword = "";
try
{
smtp = new SmtpClient("smtp.gmail.com", 587)
{
UseDefaultCredentials = false,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress, fromPassword),
Timeout = 20000,
};
message = new MailMessage(fromAddress, toAddress)
{
Subject = emailSubject,
Body = emailSubject,
};
for (int i = 0; i < filePaths.Count; i++)
{
var filePath = filePaths[i];
if (string.IsNullOrEmpty(filePath) == false)
{
attachment = new Attachment(filePath);
message.Attachments.Add(attachment);
}
}
smtp.Send(message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
ErrorLog.WriteLine(ex.Message);
}
finally
{
if (smtp != null)
{
smtp.Dispose();
}
if (message != null)
{
message.Dispose();
}
}
}
}