Vista/Win7で毎回完全に動作するように見える次のコードを使用しています。
private void SendEmail(string subject, string body, string attach)
{
using (MailMessage message = new MailMessage("username@gmail.com", "username@gmail.com", subject, body))
{
message.IsBodyHtml = true;
if (!string.IsNullOrEmpty(attach))
{
Attachment attached = new Attachment(attach);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username@gmail.com", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
client.Send(message);
}
}
ただし、Windows XP では次のようになります。
No connection could be made because the target machine actively refuses it
確認したところ、Windowsファイアウォールは完全に無効になっています...