how to send mail by using smtp in asp.net - c#

i need solution for this error
i am run that time some error occur there is:Send Email Failed.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i1sm8651517pbj.70
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
#region "Send email"
protected void btnSendmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
//Default port will be 25
smtpClient.Port = 587;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("muthu17green#gmail.com");
message.Subject = "Feedback";
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("muthu17green#gmail.com");
message.CC.Add("muthu17green#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("muthu17green#gmail.com"));
message.Bcc.Add(new MailAddress("muthu17green#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtMessage.Text;
// Send SMTP mail
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
}
}
#endregion
#region "Reset"
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
#endregion
}

You need to set the SmtpClient.Credentials property:
smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword");
This is what is used to authenticate in order to send the message. You may also need to ensure that SSL is enabled:
smtpClient.EnableSsl = true;
SmtpClient.Credentials Property MSDN Reference

It seems that you are trying to send an email using GMail, which requires SSL.
See this Google reference post.
So in your web.config, enable SSL this way:
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="smtp.gmail.com" port="587" enableSsl="true" userName="YOURUSERNAME" password="YOURPASSWORD" />
</smtp>
</mailSettings>
</system.net>
Alternatively, you can set it programmatically this way:
smtpClient.EnableSsl = true;

I think you forgot to set EnableSSL property to true which is required for gmail.
Here is the sample code:
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtFrom.Text);
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtBody.Text;
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text);
mySmtpClient.Host = "smtp.gmail.com";
mySmtpClient.Port=587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.Send(msg);
msg.Dispose();
lberr.Text="Message sent successfully";
clrtxt();
}
catch(SmtpException)
{
lberr.Text="SMTP Error handled";
}
}

public void SendMail()
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(MailTo.Text);
mail.From = new MailAddress(MailFrom.Text,"Invoice");
mail.Subject = Subject.Text;
mail.Body = Body.Text;
mail.IsBodyHtml = true;
string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName);
Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName);
mail.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("Your_Email#Email.com", "Your_Email_Password");
client.Host = "smtpout.secureserver.net";
client.Port = 80;
try
{
client.Send(mail);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

Related

How do I send email in C# app using company's email address(webmail). I tried the following codes below but didn't work

How do I send email in C# app using company's email address(webmail)? I tried the following code, but didn't work.
It works with gmail but not with webmail.
using System.Net;
using System.Net.Mail;
private void btnSend_Click_2(object sender, EventArgs e)
{
string from, pass;
MailMessage message = new MailMessage();
from = "london.city#COMPANY.org.uk";
pass = "PASSWORD";
message.To.Add("ABC#gmail.com");
message.Subject = "Hello my friend";
message.From = new MailAddress(from);
message.Body = "I am feeling good";
SmtpClient smtp = new SmtpClient("smtp.webmail.COMPANY.org.uk");
smtp.EnableSsl = true;
smtp.Port = 25;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(from, pass);
try
{
smtp.Send(message);
MessageBox.Show("send successfully");
}
catch (Exception et)
{
MessageBox.Show(et.Message);
}
}

How to send data trough email

I need to send list box items to email. Code is running fine, but when it gets to Send method it fails to send it.
protected void ProcessButton_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone#live.com");
mailMessage.From = new MailAddress("myAdress#live.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = OrderListBox.Items.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.live.com");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send email - error: " + ex.Message);
}
}
You can write list in file and send it as attachment (gmail example):
protected bool ProcessButton_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("myEmail#gmail.com", "password");
//if you have double verification on gmail, then generate and write App Password
client.EnableSsl = true;
MailMessage message = new MailMessage(new MailAddress("myEmail#gmail.com"),
new MailAddress(receiverEmail));
message.Subject = "Title";
message.Body = $"Text";
// Attach file
Attachment attachment;
attachment = new Attachment("D:\list.txt");
message.Attachments.Add(attachment);
try
{
client.Send(message);
// ALL OK
return true;
}
catch
{
//Have problem
return false;
}
}
and write this on begining of code:
using System.Net;
using System.Net.Mail;
You can choose smpt host adress and port if you want.

Cannot send email with ASP.NET

My Project need to send an e-mail but I cannot send it. I don't know why. Last month I can send but today I cannot.
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("***examplemail***", "myPassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("***examplemail***");
msg.To.Add(new MailAddress("***ToEmail***"));
msg.Bcc.Add(new MailAddress("***examplemail***"));
msg.Subject = "TEST";
msg.Body = "Hi, TEST Send E-mail";
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
** It didn't have any error but I didn't send too.
I wouldn't use the port number in here.
SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995
This way it sends. I tried and it worked. I am saying that it should look like
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you check SmtpClient's constructor while writing the code you will see that it has one overload.
Try this:
using System.Net;
using System.Net.Mail;
namespace consSendMail
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = new NetworkCredential("yourmail", "yourpassword")
};
var mailMessage = new MailMessage();
mailMessage.Subject = "Subject";
mailMessage.From = new MailAddress("yourmail", "yourname");
mailMessage.IsBodyHtml = true;
mailMessage.Body = "your message";
mailMessage.To.Add( new MailAddress("destinationemail") );
smtpClient.Send(mailMessage);
mailMessage.Dispose();
smtpClient.Dispose();
}
}
}

Sending an email programmatically through an SMTP server

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.
Server side, the relay IP addresses look correct. Scratching my head about what I'm missing.
MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);
mail.From = new MailAddress("no-reply#company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
("AdminName", "************");
smtp.EnableSsl = false;
if (fileuploadResume.HasFile)
{
mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
fileuploadResume.FileName));
}
smtp.Send(mail);
Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send.
For reference, here is my standard mail function:
public void sendMail(MailMessage msg)
{
string username = "username"; //email address or domain user for exchange authentication
string password = "password"; //password
SmtpClient mClient = new SmtpClient();
mClient.Host = "mailex.company.us";
mClient.Credentials = new NetworkCredential(username, password);
mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mClient.Timeout = 100000;
mClient.Send(msg);
}
Typically called something like this:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);
if (File.Exists(fullExportPath))
{
Attachment mailAttachment = new Attachment(fullExportPath); //attach
msg.Attachments.Add(mailAttachment);
msg.Subject = "Subj";
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.ASCII;
msg.Body = "Body";
sendMail(msg);
}
else
{
//handle missing attachments
}
var client = new SmtpClient("smtp.gmail.com", 587);
Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
EnableSsl = true
client.Send("sender mail adress","receiver mail adress", "subject", "body");
MessageBox.Show("mail sent");
This is Adil's answer formatted for C#:
public static class Email
{
private static string _senderEmailAddress = "sendermailadress";
private static string _senderPassword = "senderpassword";
public static void SendEmail(string receiverEmailAddress, string subject, string body)
{
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
EnableSsl = true
};
client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
}
catch (Exception e)
{
Console.WriteLine("Exception sending email." + Environment.NewLine + e);
}
}
}
You didn't specify the port.
smtp.Port = 1111; // whatever port your SMTP server uses
The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25.

send email from asp.net app

i configure all setting for send email using c# but when i execute than i get the following error
The requested address is not valid in its context 74.125.53.109:25
my code is
MailMessage mail = new MailMessage();
mail.To.Add("to#gmail.com");
mail.From = new MailAddress("from#gmail.com");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);
Web.Config
<appSettings>
<add key="SMTP" value="smtp.gmail.com"/>
<add key="FROMEMAIL" value="mail#gmail.com"/>
<add key="FROMPWD" value="password"/>
</appSettings>
Sending email in .NET through Gmail
This link have a full code of sending email and its proper working
sending email by gmailin my pc.
I figure I'll just post the combined effort of posts here:
Add this to your configuration file, changing the email/username/password. You may have to change the port based on what Brian Rogers posted.
<system.net>
<mailSettings>
<smtp from="some.user#gmail.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" enableSsl="true" userName="some.user#gmail.com" password="mypassword"/>
</smtp>
</mailSettings>
</system.net>
Use this in your code
MailMessage mail = new MailMessage();
mail.To.Add("to#gmail.com");
mail.From = new MailAddress("from#gmail.com");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
Port 25 is the default port but not the correct port for sending email over SSL. Since you are using SSL, you will need to set smtp.Port = 465, according to Google's help page on the topic: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
I think the address is not valid because it targets port 25 in the context of an SSL connection.
I did this already and I have already tested it out in Gmail.
StackOverflow - Send email with attachments
Gmail port = 465
Use SSL = true
You cant specify port 25 with :25 after the IP.
It will default to port 25 so you don't need it. If you want to change the port use the following:
mail.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"portnumber"
);
This is the function which works well for send mail, i had checked it and it's working.
private static bool testsendemail(MailMessage message)
{
try
{
MailMessage message1 = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
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" + message;
// We use yahoo as our smtp client
smtpClient.Host = "smtp.mail.yahoo.com";
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;
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
protected void btnSend_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("xxx#yourdomain.com");
msg.To.Add(txtTo.Text); //Text Box for To Address
msg.Subject = txtSubject.Text; //Text Box for subject
msg.IsBodyHtml = true;
msg.Body = txtBody.Text; //Text Box for body
msg.Priority = MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(
"relay-hosting.secureserver.net",
25
);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(
"xxx#yourdomain.com",
"yourpassword"
);
client.Host = "relay-hosting.secureserver.net";
client.EnableSsl = false;
object userstate = msg;
client.Send(msg);
}
}

Categories