when sending an email, I get an exception:
Sending mail failed.
I've set the host to "smtp.gmail.com" and port to 587 but it doesn't work.
Code:
public void SendActivationLink(string email)
{
using (MailMessage mail = new MailMessage())
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
MailMessage message = new MailMessage();
MailAddress from = new MailAddress("myemail#gmail.com", "Voc");
message.From = from;
message.To.Add(email);
message.Subject = "Title";
message.Body = "Jello";
smtpClient.Host = "smtp.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
try
{
smtpClient.SendAsync(message, email);
}
catch (SmtpException ex)
{
throw new ApplicationException("exeption" + ex.Message);
}
}
Related
I have update from an obsolete name space to System.Net.Mail - the code was supposed to be straight forward - I am having problems sending email and can't get hold of the issue
public bool send()
{
SmtpClient mailClient = new SmtpClient("my domain", 25);
MailMessage mailMessage = new MailMessage();
mailClient.EnableSsl = false;
mailMessage.From = new MailAddress("my email");
mailMessage.To.Add(sendTo.ToString());
//mailMessage.Bcc.Add(bcc.ToString());
//mailMessage.CC.Add(cc.ToString());
mailMessage.Subject = subject;
mailMessage.Body = body.ToString();
mailMessage.IsBodyHtml = true;
try
{
mailClient.Send(mailMessage);
}
catch (Exception exp)
{
exp.ToString();
return false;
}
return true;
}
using Gmail smtp server
var client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
if (!client.UseDefaultCredentials)
client.Credentials = new System.Net.NetworkCredential("your_email#mail.com", "your_email_pass");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
var mail = new MailMessage("from#mail.com", "to#mail.com");
mail.Subject = "test ";
mail.Body = "body message";
mail.IsBodyHtml = true;
client.Send(mail);
No Credential for the smtp server? Or you add it already.
Try
mailClient.Credentials = new NetworkCredential("username", "password"),
I'm attaching the code below:
I get an Error in this Code But This Code Perfectly Working at My Local
IIS.But not Working at Any Server,
My Code was
Email-Code
MailMessage msg = new MailMessage();
msg.From = new MailAddress("From Mail ID");
msg.To.Add("To Mail ID");
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("From Mail ID", "******");
client.Timeout = 20000;client.Send(msg);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("*****#gmail.com");
mail.From = new MailAddress("domain email");
mail.Subject = "Test Email";
mail.IsBodyHtml = true;
mail.Body = "Test";
SmtpServer.Host = "dedrelay.secureserver.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try {
SmtpServer.Send(mail);
} catch (Exception ex) {
throw ex;
}
I've tried sending mails using this code:
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
MailMessage mail = new MailMessage("email#gmail.com", "some1#gmail.com", "Subject", "Body");
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mail);
It doesn't work, the error looks like this: http://scr.hu/5gpw/tv6hn
What's wrong with this code?
I would change the code into a Method and test passing in the the Port, Host etc look at this and see if it works for you I just tested it and it works great on my end.
public void Send(string from, string to,string smtpServer, int smtpPort,string username, string password)
{
try
{
using (MailMessage mm = new MailMessage())
{
SmtpClient sc = new SmtpClient();
mm.From = new MailAddress(from, "Test");
mm.To.Add(new MailAddress(to));
mm.IsBodyHtml = true;
mm.Subject = "Test Message";
mm.Body = "This is a test email message from Krzysztof Senska";
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.SubjectEncoding = System.Text.Encoding.UTF8;
NetworkCredential su = new NetworkCredential(username, password);
sc.Host = smtpServer;
sc.Port = smtpPort;
sc.Credentials = su;
sc.Send(mm);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void SendEmailWithAttachment(string pFrom, string pTo, string pSubject, string pBody, string pServer, string strAttachmentPDFFileNames)
{
try
{
System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
string UserName = ConfigurationManager.AppSettings["SMTPUserName"];
string Password = ConfigurationManager.AppSettings["SMTPPassword"];
if (pTo.Contains(","))
{
string[] ToAdd = pTo.Split(new Char[] { ',' });
for (int i = 0; i < ToAdd.Length; i++)
{
Message.To.Add(ToAdd[i]);
}
}
else
{
Message.To.Add(pTo);
}
//System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(pTo);
//Message.To.Add(toAddress);
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(pFrom);
Message.From = fromAddress;
Message.Subject = pSubject;
Message.Body = pBody;
Message.IsBodyHtml = true;
// Stream streamPDFImages = new MemoryStream(bytPDFImageFile);
//System.Net.Mail.SmtpClient
var smtpClient = new System.Net.Mail.SmtpClient();
{
Message.Attachments.Add(new System.Net.Mail.Attachment(strAttachmentPDFFileNames));
smtpClient.EnableSsl = true;
smtpClient.Host = pServer;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.Port = 465;
smtpClient.Send(Message);
}
}
catch (Exception Exc)
{
Exception ex = new Exception("Unable to send email . Please Contact administrator", Exc);
throw ex;
}
}
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("yourMail", "yourPassword");
smtp.EnableSsl = true;
MailMessage msg = new MailMessage(sendFrom, "yourMail");
msg.ReplyToList.Add(sendFrom);
msg.Subject = subject;
msg.Body = bodyTxt;
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(#"C:\Projects\EverydayProject\test.txt");
msg.Attachments.Add(attachment);
smtp.Send(msg);
Here is working code to send an email to gmail smtp. From what I see you don't set UseDefaultCredentials = false and you are using wrong port. Also you MUST NOT override exceptions like this., throw the initial exception. Also for this to work you need to turn off security setting in your Gmail account. You can google it.
I'm trying to send an e-mail in C#,and I'm having some issues.Whenever I try to send an e-mail,I get a message "Error: Failure to send mail".
Here is my code:
try
{
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
MailAddress sendFrom = new MailAddress("from#domain.com");
MailAddress sendTo = new MailAddress("to#domain.com");
MailMessage msg = new MailMessage(sendFrom,sendTo);
msg.Subject = "Subject";
msg.Body = "Body";
client.Send(msg);
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message);
}
Also I have this declaration:
public SmtpClient client = new SmtpClient();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
Hope you can help me.
you can try this and make sure you are using valid login credential and you have internet connection:
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
Try this code
using System.Net.Mail;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("sender#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = ("e mail subject");
mail.Body = ("message body");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sender's username", "sender's password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");