Sometimes following code sends Email,Sometimes i get SMTP Exception saying "failure in sending Email".Why is this happening? following is the code i have written.Plesae tell me why sometime i get email sent and sometime why i get Exception.
StringBuilder mailBody = new StringBuilder();
mailBody.Append("Hi <br/>")
MailMessage message = new MailMessage("from#gmail.com", "to#gmail.com");
message.Subject = "Subject";
message.Body = mailBody.ToString();
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
smtpclient.Credentials = new System.Net.NetworkCredential()
{ UserName = "from#gmail.com",
Password = "password"
};
smtpclient.EnableSsl = true;
message.IsBodyHtml = true;
smtpclient.Send(message);
Related
I am using below code to send email to webmail GoDaddy and it is working fine from my local machine and when deployed on client server it is not working.
Below is my code :
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
MailAddress fromAddress = new MailAddress("xyz#com");
message.From = fromAddress;
message.To.Add("abc#gmail.com");
message.Subject = "test Message";
message.IsBodyHtml = true;
message.Body = "we are here";
smtpClient.Host = "mail.trustwellhospitals.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential("username", "password");
smtpClient.Send(message);
i'm unable to send mail, exception "Failure sending mail"
what should i do to correct the code?
MailMessage mailMessage = new MailMessage("yourmail#mail.com", ToEmail);
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("<b>Registration Approval for</b></br>");
sbEmailBody.Append(InstituteName + "<br/><br/>");
sbEmailBody.Append("Thanks for your registration ");
sbEmailBody.Append("<br/>");
mailMessage.IsBodyHtml = true;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.Subject = "Confirmation Mail!";
SmtpClient smtpClient = new SmtpClient("smtp.mail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "yourmailn#mail.com",
Password = " "
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
When i send email there is an exception which says "failure sending mail".
var mailObj = new MailMessage("marabifuad2013#gmail.com", "to Email");
mailObj.Subject = "subject";
mailObj.Body ="";
mailObj.IsBodyHtml = true;
var smtpServer = new SmtpClient("smtp.gmail.com", 587);
smtpServer.Credentials = new NetworkCredential("marabifuad2013#gmail.com", "password");
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Send(mailObj);
I guess that because you haven't specified to use SSL:
smtpServer.EnableSsl = true;
Also you can try:
smtpServer.UseDefaultCredentials = true;
Apart from that everything looks fine, providing that you entering correct credentials and etc.
I have functionality in my site which sends a email to an id .. Email contains a link to a webpage and a security key .. But the problem is it goes into the junk folder
I'm using free hosting by Somee.com
Code:
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = ("Copy The Link And paste It In Them follow Link Download </br>"+ encoded_url);
message.From = new MailAddress("lz-wag#hotmail.com");
message.To.Add(TextBox2.Text);
message.Subject = user + " Has Share The File With You";
try{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com";
client.EnableSsl = true;
System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
networkcred.UserName = "lz-wag#hotmail.com";
networkcred.Password = "password";
client.Port = 587;
client.Credentials = networkcred;
client.Send(message);
sendFile.Visible = false;
Label1.Visible = true;
Label1.Text = "Your File Has Been Shared";
}
catch(Exception ex){
Label1.Visible = true;
Label1.Text = "Your File Is Not Shared";
//Label1.Text = ex.ToString(); ;
}
Whether or not the email goes into the junk mail folder is a function of the email client, not a function of how you are sending the email.
However, FYI, both the MailMessage and the SmtpClient implement IDisposable, so should be in using blocks. Something like this:
using (MailMessage message = new MailMessage())
{
// ...
using (SmtpClient client = new SmtpClient())
{
// ...
client.Send(message);
}
}
I also suggest that you log the exception somewhere, or you'll never know what went wrong when something goes wrong.
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);