Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Email
- {
- public Email(string password, string username)
- {
- this.username = username;
- this.password = password;
- }
- private string username;
- private string password;
- public bool Send(string toAddress, string subject,
- string body, string attachment)
- {
- // Create the message
- System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
- // Add all the addresses
- message.To.Add(new System.Net.Mail.MailAddress(toAddress));
- // Set the from address
- message.From = new System.Net.Mail.MailAddress(this.username);
- // Set the subject
- message.Subject = subject;
- // Set the subject encoding
- message.SubjectEncoding = Encoding.UTF8;
- // Set the body
- message.Body = body;
- // Set the body encoding
- message.BodyEncoding = Encoding.UTF8;
- // The body is not an HTML
- message.IsBodyHtml = false;
- // Set the priority
- message.Priority = System.Net.Mail.MailPriority.High;
- // Connect to Gmail
- System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
- smtpClient.UseDefaultCredentials = false;
- smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
- // Set the credentials
- smtpClient.Credentials = new System.Net.NetworkCredential(this.username, this.password);
- // Set the port
- smtpClient.Port = 25;
- // Set the host
- smtpClient.Host = "smtp.gmail.com";
- // Enable SSL
- smtpClient.EnableSsl = true;
- // Add attachments
- message.Attachments.Add(new System.Net.Mail.Attachment(attachment));
- // Send the message
- try
- {
- smtpClient.Send(message);
- return true;
- }
- catch
- {
- message.Attachments.Dispose();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement