I am trying to setup an email sending application, using a hotmail account.
The code looks like this:
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Proba email";
mail.Attachments.Add(new Attachment("C:\\Documents and Settings\\Proba.txt"));
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Port = 587; // 465 568
client.Host = "smtp.live.com";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("smg@hotmail.com", "password");
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(mail, "token");
Using, Async I actually get no errors, I even get the feedback saying message sent (Event triggers) but the message never arrives. If I use the simple client.Send void, I get the following error:
5.3.4 Requested action not taken; To continue sending messages, please sign in to your account.
So any ideas on wha开发者_如何学Pythont the problem can be? As I was trying to hand down the SMTP settings of hotmail I got various setups saying port 25, then 587 so maybe it's something there. Any help would be greatly appreciated thanks!
- Okay so it's definitely working now, I would just like to ask if I will have to do regular "I'm not a robot checks" or was that a one-time thing?
Here is my setup, BTW async will not return any errors.
<system.net>
<mailSettings>
<!-- E-mail server settings -->
<smtp from="do-not-reply@example.com">
<network host="smtp.example.com" port="25" userName="" password="" defaultCredentials="true" />
</smtp>
</mailSettings>
void SendEmail(EmailEntity email)
{
var mailMessage = new MailMessage { From = new MailAddress(email.From) };
mailMessage.To.Add(new MailAddress(email.To));
mailMessage.Subject = email.Subject;
mailMessage.Body = email.Body;
mailMessage.IsBodyHtml = true;
// Send the email
var client = new SmtpClient();
client.Send(mailMessage);
}
I have a similar setup where I just use Send() that is working for me. The only thing extra that I have is
client.DeliveryMethod = SmtpDeliveryMethod.Network;
just before client.Send();
. Not sure if this alone will solve your problem. Do you have outlook setup with the same server and credentials ?
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
// System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
MailAddress fromAddress = new MailAddress(txt_name.Text, txt_to.Text);
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
// msg.From = new System.Net.Mail.MailAddress("xyz@gmail.com");
message.From = fromAddress;
message.To.Add("xyz111@gmail.com");
message.Body = txt_des.Text;
smtpClient.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xyz@gmail.com";
NetworkCred.Password = "xtz";
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}
精彩评论