E mail sending error Asp.net C# - c#

Iam geyying Error like
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
my code is
protected void SendEmail_Click(object sender, EventArgs e)
{
// reading info from web.config file
string HostAdd = ConfigurationManager.AppSettings["host"].ToString();
string Sender = ConfigurationManager.AppSettings["MyEmail"].ToString();
string MyPassword = ConfigurationManager.AppSettings["Password"].ToString();
//creating MailBox Object
MailMessage Email = new MailMessage();
Email.From = new MailAddress(Sender);
Email.Subject = "Interview";
Email.Body = "EmailBodytxt.Text";
Email.IsBodyHtml = true;
//Gettting All Shortlisted Applicants Email ID
string JobID = Request.QueryString["Qstring"];
string Query = "select E_mail from Users inner join Apply on Apply.Applicant_CNIC =Users.User_ID where Users.Role='Applicant' and Apply.Status='Shortlisted' and Apply.Job_ID=" + JobID + "";
DataTable EmailDT = DataTableClass.GetDataset(Query);
foreach (DataRow E_Address in EmailDT.Rows)
{
string ID = E_Address["E_Mail"].ToString();
// Email.To.Add(new MailAddress(E_Address["E_Mail"].ToString()));
Email.To.Add(new MailAddress(ID));
}
SmtpClient smtp = new SmtpClient();
smtp.Host = HostAdd;
//network and security Managment
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = sender.ToString();
NetworkCred.Password = MyPassword;
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(Email);//sending Email
//client.Send(msg);
}

Related

SMTP server not getting credentials from C# code

I'm trying to set up an automatic email sender.
My smtp server requires authentication. I'm not using ssl but still I'm getting the following response from server:
The server response was: 5.7.1 You are not authorized, authentication is required.
And if I check on the server it got an empty username.
Here is my code:
public void email_send(string mailadress,string docname)
{
string host = "myhost";
string username = "user";
string password = "password";
int port = 625;
var nc = new NetworkCredential(username, password);
var auth = nc.GetCredential(host,port,"Basic");
using (var mail = new MailMessage())
using (var SmtpServer = new SmtpClient())
{
SmtpServer.Host = host;
SmtpServer.Port = port;
SmtpServer.Credentials = auth;
mail.From = new MailAddress("fromadress");
mail.To.Add(mailadress);
mail.Subject = "Subject";
mail.Body = "Body";
Attachment attachment;
attachment = new Attachment("D:/" + docname + ".pdf");
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Send(mail);
}
}

C# receive error sending a email

ERROR: Service not available, closing transmission channel. The server response was: Resources temporarily unavailable - Please try again later
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "mail#yahoo.com";
string password = "mailpass";
string emailTo = "sendemail#yahoo.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
//mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
//mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
What should I do?
You need to make sure an external application has permission to send emails through your email before you can proceed.

the operation has timed out system net mail smtpexception

I want to send mail using mail web application. While sending mail showing time out error. Help me to find a proper solution.
protected void btnsubmit_Click(object sender, EventArgs e)
{
Ticket_MailTableAdapters.tbl_TicketTableAdapter tc;
tc = new Ticket_MailTableAdapters.tbl_TicketTableAdapter();
DataTable dt = new DataTable();
dt = tc.GetEmail(dpl_cate.SelectedValue);
foreach (DataRow row in dt.Rows)
{
string eml = (row["Emp_Email"].ToString());
var fromAddress = "myMail#gmail.com";
var toAddress = eml;
const string fromPassword = "*****";
string body = "Welcome..";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
smtp.Timeout = 600000;
}
}
Use This Working Code.
const string vMailFm = "myMail#gmail.com";
var vMailTo = ((txtEmailId.Text == "") ? "myMail#gmail.com" : txtEmailId.Text);
MailMessage vMail = new MailMessage(vMailFm, vMailTo);
const string vSubject = "Center Detail From SAFE";
var vDetails = "";
vDetails += "Dear User,<br><br>";
vDetails += "Kindly find the user details of your registration with SAFE<br><br>";
vDetails += "Your UserName Is - " + vMailTo + "<br>";
vDetails += "Your Password Is - " + Convert.ToString(ViewState["password"]) + "<br>";
vDetails += "<br>";
vDetails += "Registration has been Successfully Completed....";
vDetails += "<br><br>";
vMail.Subject = vSubject;
vMail.Body = vDetails;
vMail.IsBodyHtml = true;
SmtpClient vSmpt = new SmtpClient();
System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("myMail#gmail.com", "Password123");
vSmpt.Host = "smtp.gmail.com";
vSmpt.Port = 587;//for local
// vSmpt.Port = 25;//for online
vSmpt.EnableSsl = false;
vSmpt.DeliveryMethod = SmtpDeliveryMethod.Network;
vSmpt.UseDefaultCredentials = false;
vSmpt.Credentials = smtpUser;
vSmpt.Send(vMail);

Time out error while sending mail to gmail account using smtp port 465 in asp.net

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.

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.

Categories