Failure sending e-mail in asp.net - c#

Here's my code. I know that this is a duplicate question but I have visited all the links in staqckoverflow and codeproject but still I am unable to get an answer.
try
{
MailMessage mailmessage = new MailMessage();
mailmessage.To.Add("xxx.com");
mailmessage.From = new MailAddress("xxx.com");
mailmessage.Subject = "Transaction sent for Verification ";
mailmessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient = new SmtpClient(ConfigurationSettings.AppSettings["SmtpAdd"].ToString());
smtpClient.Send(mailmessage);
ClientScript.RegisterStartupScript(this.GetType(), "mailalert", "alert('" + "Mail sent for verification" + "');", true);
}
catch(Exception ex)
{
//Response.Write("Could not send E-mail- error: " + ex.Message);
ClientScript.RegisterStartupScript(this.GetType(), "mailfailure", "alert('" + ex.Message + "');", true);
}

This will help you
MailMessage msg = new MailMessage();
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress("To_address"));
msg.Subject = "You have a WebMail!!";
msg.IsBodyHtml = true;
msg.Body = "Message";
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("your mail address", "your password");
smtp.Host = "host name";
smtp.Port = "host number";
smtp.EnableSsl = false;
smtp.Send(msg);

Related

Email configuration

Hi guys I am doing email function for the first time and I searched up on how to do a email
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 587;
smtp.Host = "gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential("from#gmail.com", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
I tried running this but I am not sure what is the problem... i did not use IIS or anything
(update)
I uploaded the error image in this website
http://imgur.com/3vVZhsW
This one will Help you..! try this..!
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
SmtpClient smtp = new SmtpClient();
//smtp object.
Msg.From = new MailAddress("from#gmail.com");
// Recipient e-mail address.
Msg.To.Add(to#gmail.com);
//here you can pass textbox value how need to send mail.
Msg.Subject = "Please confirm your subscription";
Msg.Body = "<body></body>";
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("from#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
}
catch(Exception ex)
{
Message.Show("Error:" + ex.Message);
}

Gmail mail cannot be sent through C#

I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.
MailMessage message = new MailMessage();
message.From = new MailAddress("Selvacsebe23#gmail.com");
string[] mailaddress = new string[count];
int i;
if (textSubject.Text != string.Empty)
{
message.Subject = textSubject.Text;
if (textBody.Text != string.Empty)
{
message.To="Selvakesavan#gmail.com"
message.IsBodyHtml = true;
string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
message.Body = tmpBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("selvacsebe23#gmail.com", "mypassword");
message.Priority = MailPriority.High;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Mail has sent successfully !!!", "Success !");
}
else
{
MessageBox.Show("Please Enter Body of the Message !");
}
}
else
{
MessageBox.Show("Please Enter Subject !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failure !");
log.Fatal(ex.Message);
}
}
If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here:
https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:
const string from = "user1#gmail.com";
const string to = "user2#yahoo.com";
const string subject = "This is subject";
const string body = "This is body";
const string appSpecificPassword = "akdfkajsdhklakdfh";
var mailMessage = new MailMessage(from, to, subject, body);
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
smtpClient.Send(mailMessage);
}

Send mail from Exchange Server

I want to send mail from exchange server without password. I use the following code snippet :
MailMessage msg = new MailMessage();
msg.From = new MailAddress("kavitham#test.com");
msg.To.Add(new MailAddress("kavitham#sample.com"));
msg.Subject = "Reg : Mail test";
msg.IsBodyHtml = true;
msg.Body = "<html><body>" + strMsg + "</body></html>";
SmtpClient client = new SmtpClient("IP of Server", 25);
client.Host = "IP of Server";
client.Send(msg);
But the mail is not sent. Is there any other settings for Exchange server need to be done ?
add this
smtpClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
MailMessage msg = new MailMessage("noreply#test.com", "manager#test.com", "Notification For Lead / Admin ",
"Emp Name :" + EmpName);
msg.Body = "Emp Name : " + EmpName + "<br /><br >" + Environment.NewLine + timsheetMail;
msg.IsBodyHtml = true;
msg.CC.Add(toMail); msg.CC.Add(admin);
SmtpClient SMTPServer = new SmtpClient("mail.myserver.com", 25);
SMTPServer.Timeout = 300000;
SMTPServer.EnableSsl = false;
SMTPServer.Credentials = new System.Net.NetworkCredential("noreply#test.com", "passw0rd");
SMTPServer.Send(mailObj);

C# Windows Form Application - Send email using gmail smtp

I have been trying to create a small program to send email through smtp.gmail.com, but it always prompt me that "The operation has timed out".
I know there are lots of solutions available on the net but none of it works.
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 465;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("from#gmail.com", "pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
Is there any way to solve this?
Change the port to 587:
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("from#gmail.com", "pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
how to how to send email of pdf file which is store in d drive in c# windows
application...the answer is...
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(txtFrom.Text.ToString());
mail.To.Add(txtmailTo.Text.ToString());
mail.Subject = "Mail Pdf";
var filename = #"D:/your file path/.pdf";
mail.Attachments.Add(new Attachment(filename));
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential(txtFrom.Text, txtPassword.Text);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
you can use SMTP protocol to send this image as an Attachment, your code will be something like that :
using MailKit.Net.Smtp; using MimeKit;
MimeMessage message = new MimeMessage();
BodyBuilder Attachmint = new BodyBuilder();
message.From.Add(new MailboxAddress("name sender", "Mail From"));
message.To.Add(MailboxAddress.Parse("Mail To"));
message.Subject = Subject;
message.Body = new TextPart("plain")
{
Text = tex_body.Text + Massage
};
Attachmint.Attachments.Add("Attatchment Path");
message.Body = Attachmint.ToMessageBody();
SmtpClient client = new SmtpClient();
client.Connect("smtp.gmail.com", 465, true);
client.Authenticate("Mail from", "Password mail");
client.Send(message);

Sending e-mail in asp with usings

I have an email form on my website which is working great. The only thing I want to do is add usings for better coding, what is the best way to do this? The code I use is the following:
try
{
//create message
MailMessage msg = new MailMessage();
msg.To.Add(txtTo.Text);
msg.From = new MailAddress(txtFrom.Text);
msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
msg.Body = txtBericht.Text;
SmtpClient smtp = new SmtpClient("");
smtp.Host = "smtp.live.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("emailaddress", "pass");
smtp.Send(msg);
ClearTextboxes();
lblError.ForeColor = Color.Green;
lblError.Text = "Het verzenden van uw e-mail is gelukt!";
}
catch
{
lblError.ForeColor = Color.Red;
lblError.Text = "Er ging iets mis met het versturen van de email.";
}
How can I use usings on this code?
And is it safe to use fill in my emailaddress and password when I'm uploading my website, or should I use another method?
Thanks in advance!
EDIT:
I changed my code to the following:
try
{
//create message
using (MailMessage msg = new MailMessage())
{
//create message
MailMessage msg = new MailMessage();
msg.To.Add(txtTo.Text);
msg.From = new MailAddress(txtFrom.Text);
msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
msg.Body = txtBericht.Text;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.live.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("emailadress", "pass");
smtp.Send(msg);
}
ClearTextboxes();
lblError.ForeColor = Color.Green;
lblError.Text = "Het verzenden van uw e-mail is gelukt!";
}
}
catch
{
lblError.ForeColor = Color.Red;
lblError.Text = "Er ging iets mis met het versturen van de email.";
}
Never put authentication information in your code. Put it in your web.config instead and reference it where you need it. That also saves you the trouble of hunting down everywhere you used it if you change your password.
I would suggest using the web.config for setting up the host, username and password. Example.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="name#domain.com">
<network host="smtp.mail.com"
userName="name#domain.com"
password="blog.dotnetclr.com" port="25"/>
</smtp>
</mailSettings>
</system.net>
Code Example:
Public Sub SendEmail([to] As String, subject As String, body As String)
Dim mailMessage = New System.Net.Mail.MailMessage()
mailMessage.[To].Add([to])
mailMessage.Subject = subject
mailMessage.Body = body
Dim smtpClient = New SmtpClient()
smtpClient.EnableSsl = True
smtpClient.Send(mailMessage)
End Sub
http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.100).aspx
using (MailMessage msg = new MailMessage())
{
//Your Code
}
You could use using for both MailMessage and SMTP Client objects
//create message
using (SmtpClient smtp = new SmtpClient(""))
using( MailMessage msg = new MailMessage())
{
// Your code
}
using(SmtpClient smtp = new SmtpClient()){
//smtp options here
using(MailMessage msg = new MailMessage()){
//mail options here
smtp.Send(msg);
}
}

Categories

Resources