We are working in Mailing system in asp.net,our client using Microsoft exchange server outlook.office365. We are getting this exception.
"System.Net.Mail.SmtpException: The operation has timed out " error .Please help us.
btn_click(){
try{SmtpClient smtp = new SmtpClient("smtp.outlook.office365.com");
smtp.Host = "outlook.office365.com";
smtp.Port = System.Convert.ToInt32("443");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("userid", "mypwd");
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.TargetName = "STARTTLS/outlook.office365.com";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("frommailid", "username");
msg.To.Add(new MailAddress("xxx#xxx.com"));
msg.Subject = "Test";
msg.Body = "Test mail ";
smtp.Timeout = 60000;
smtp.Send(msg);
}catch(exception ex)
{
throw ex;
}
}
Refer the below code snippet and try this out. It worked for me.
Remember: From field should be same email id which is used as an SMTP user. So in this example, xxx#blabla.onmicrosoft.com should be the From email id.
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(ToField.Text));
mail.From = new MailAddress(FromField.Text); //From field should be same email id which is used as a SMTP user.
mail.Subject = "Email Subject";
mail.Body = "Message Body";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxx#blabla.onmicrosoft.com", "Password");
client.Port = 587;//You can use Port 25 if 587 is blocked
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.TargetName = "STARTTLS/smtp.office365.com";
try {
client.Send(mail);
}
catch (Exception ex) {
ResponseLabel.Text = "Error:" + ex.Message;
}
Related
the email code works on local host but when i upload the website on server it shows error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
//Formatted
protected void btnSend_Click(object sender, EventArgs e)
{
var fromAddress = "djdanny1255#gmail.com";
string email = "djdanny1255#gmail.com";
var toAddress = email;
const string fromPassword = "********";
string subject = "Email=" + txtEmail.Text + " Phone=" + txtMobile.Text;
string body = txtMessage.InnerText;
try
{
using (MailMessage mm = new MailMessage(fromAddress, email))
{
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
catch (Exception ex)
{
Response.Write("Error" + ex.Message);
}
in first cas, i suggest you to change code at this code :
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress,fromPassword);
smtp.Send(mail);
}
}
and second after your Login in to your email, CLICK HERE .
This will see this page
i hope this help you ^^
mailMessage.From = new System.Net.Mail.MailAddress("abc#yourserver.com");
mailMessage.Subject =TextBox3.Text;
mailMessage.Body = TextBox5.Text;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new System.Net.Mail.MailAddress(TextBox4.Text));
mailMessage.CC.Add(new System.Net.Mail.MailAddress("xyz#gmail.com"));
System.Net.Mail.SmtpClient smtp = new SmtpClient();
smtp.Host = "smtpout.secureserver.net";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "abc#yourserver.com";
NetworkCred.Password = "1234566";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mailMessage);
error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.201.193.228:587
Here is an exapmple of how ASP.NET Identity messages can be sent, but you can use it for any purposes. It is using an external SMTP server to operate (gmail in this case).
public Task SendAsync(IdentityMessage message)
{
try
{
// Plug in your email service here to send an email.
using(MailMessage mail = new MailMessage())
{
mail.To.Add(message.Destination);
mail.From = new MailAddress(WebConfigurationManager.AppSettings["accountsServerEmail"]);
mail.Subject = "Mobiler-registration";
mail.Body = message.Body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = WebConfigurationManager.AppSettings["smtpServerPath"];
smtp.Port = Convert.ToInt32(WebConfigurationManager.AppSettings["smtpServerPort"]);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(
WebConfigurationManager.AppSettings["smtpServerRegisterCredentialsLogin"],
WebConfigurationManager.AppSettings["smtpServerRegisterCredentialsPassword"]);// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
return Task.FromResult(1);
}
}
}
catch (Exception e)
{
throw;
}
}
I want to attach a file dynamically and send it through mail. So can someone please let me know how can i send it with attachment.
You can use this code.
using System.Net.Mail;
private void SendEmail()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("YourEmailAddress#domain.com");
message.To.Add(new MailAddress("Recipient#domain.com"));
message.Subject = "Subject";
message.Body = "Email Message Body";
// Add attachment
string attachmentPath = Server.MapPath("~/AttachmentPath.jpg");
message.Attachments.Add(new Attachment(attachmentPath));
// Connect to GoDaddy SMTP
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net");
smtp.Credentials = new System.Net.NetworkCredential("Username","Password");
smtp.Port = 25;
smtp.EnableSsl = false;
// Send the message
smtp.Send(message);
}
you can try with this code
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("...");/:Adjust your adress
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);
SmtpServer.Port = ..; //Replace with your port number
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
i copy that code
MailMessage mail = new MailMessage();
mail.To.Add(model.To);
mail.From = new MailAddress("sheikh.abm#gmail.com");
mail.Subject = model.Subject ;
mail.Body = model.Message;
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost:");
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Host = "localhost:";
client.Port = 25;
NetworkCredential credentials = new NetworkCredential(model.To, "abc");
client.Credentials = credentials;
try
{
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
client.Send(mail);
}
catch
{
}
return View("Index");
}
the exception i got is "SSL must not be enabled for pickup-directory delivery methods"
.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No connection could be made because the target machine actively refused it?
I Face Critical Problem During Sending Mail in Asp.net please tell me Solution if Anyone know About this.
This Is My Code for Sending Mail
MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient();
mm.From = new MailAddress(txtfrom.Text);
mm.To.Add(new MailAddress(txtto.Text));
mm.Subject = txtsubject.Text;
mm.Body = txtmessage.Text;
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com"; //You can add this in the webconfig
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "mymail#gmail.com";
NetworkCred.Password = "my password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //this is Gmail port for e-mail
smtp.Send(mm);//send an e-mail
The Problem is That when i Click on Send Button it Show following Error
"No connection could be made because the target machine actively
refused it 173.194.79.109:587"
Please tell me Solution ..
you need to set false the UseDefaultCredentials property.
smtp.UseDefaultCredentials = false;
That error message usually means a firewall is blocking your connection. Instead of using your machine name "173.194.79.109:587", try using "localhost".
protected void SendMail_Click(object sender, EventArgs e)
{
var fromAddress = new MailAddress(fromid.Text, fromname.Text);
var toAddress = new MailAddress(toid.Text, toname.Text);
string fromPassword = pswd.Text;
string subject = subjectbox.Text;
string body = bodybox.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = false
})
{
smtp.Send(message);
}
}
This is the function which i checked to send mail...and it's working properly.
`
private static bool testsendemail(MailMessage messageBody)
{
try
{
MailMessage message1 = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
MailAddress fromAddress = new MailAddress("FromMail#Test.com");
message1.From = fromAddress;
message1.To.Add("ToMail#Test1.com");
message1.Subject = "This is Test mail";
message1.IsBodyHtml = true;
message1.Body ="You can write your body here"+messageBody;
smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("SenderMail#yahoo.com", "YourPassword");
smtpClient.Send(message1);
}
catch
{
return false;
}
return true;
}`
Thank You.
This is how I use it.
SmtpClient gglClient = new SmtpClient("173.194.67.108", 587);
gglClient.UseDefaultCredentials = false;
gglClient.DeliveryMethod = SmtpDeliveryMethod.Network;
gglClient.Credentials = new NetworkCredential("username#gmail.com", "password");
gglClient.EnableSsl = true;
MailMessage msg = new MailMessage("", email);
msg.Subject = "";
msg.Body = #" ";
msg.IsBodyHtml = false;
msg.BodyEncoding = UTF8Encoding.UTF8;
gglClient.Timeout = 8000;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; };
gglClient.Send(msg);