Sending an email via Google Apps SMTP Server c#
I am currently developing an application in C# using WPF.
I need the program to be able to send an email to the users email account. The email needs to be sent through my own SMTP server which is using the free Google Apps version for email.
I have put the following code in order to send the message.
try
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port =开发者_StackOverflow中文版 465;
smtpClient.Credentials = new NetworkCredential("myusername", "mypassword");
smtpClient.EnableSsl = true;
MailMessage message = new MailMessage();
message.To.Add(getEmail());
message.Subject = "Password Manager Sync Account Created";
message.From = new MailAddress("username@domain.com");
message.Body = "My Email message"
smtpClient.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("Error Occurred" + ex.Message, "Email Failed", MessageBoxButton.OK, MessageBoxImage.Error);
However, when this code runs it instead just displays an error to say that the operation timed out.
What could be wrong with this?
I have a program that uses SMTP and it goes through port 587 on gmail. Try that.
Here is some code that works for me (forgot where I found it):
public class GmailService : IEmailService
{
private static int _port = 465;
private readonly string _accountName;
private readonly string _password;
public GmailService(string accountName, string password)
{
_accountName = accountName;
_password = password;
}
public void Send(string from, string to, string subject, string body, bool isHtml)
{
Send(from, to, subject, body, isHtml, null);
}
public void Send(string from, string to, string subject, string body, bool isHtml, string[] attachments)
{
System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage
{
From = from,
To = to,
Subject = subject,
Body = body,
BodyFormat = isHtml ? MailFormat.Html : MailFormat.Text
};
// Add attachments
if (attachments != null)
{
for (int i = 0; i < attachments.Length; i++)
{
mailMessage.Attachments.Add(new Attachment(attachments[i]));
}
}
// Authenticate
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
// Username for gmail - email@domain.com for email for Google Apps
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _accountName);
// Password for gmail account
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password);
// Google says to use 465 or 587. I don't get an answer on 587 and 465 works - YMMV
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _port.ToString());
// STARTTLS
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
// assign outgoing gmail server
SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(mailMessage);
}
}
accountName is something like 'bobcravens@yourserver.com' password is the password for that account.
Hope this gets you started.
Bob
精彩评论