Getting Error While Sending Mail on client.send(message)? - asp.net

I have created one mail.aspx.cs with following code
protected void Page_Load(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("sender#foo.bar.com");
message.To.Add(new MailAddress("recipient1#foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
}
And web.config settings
<system.net>
<mailSettings>
<smtp from="test#gmail.com">
<network host="smtpserver1" port="25" userName="abc xyz" password="abc" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Getting failure error while sending mail on page load on following line
client.Send(message); mail sending fail,
i am new in asp.net,guide me correct way to solve this problem.

message.From = new MailAddress("sender#foo.bar.com");
And
<mailSettings>
<smtp from="nitinturankar3#gmail.com">
Must be the same!

You are using a username with empty space userName="NITIN TURANKAR", this is wrong

public string SendMail(string toList, string from, string ccList, string subject, string body)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
//if (attachments != "")
//{
// message.Attachments.Add(new Attachment(attachments));
//}
if (ccList != null && ccList != string.Empty)
message.Bcc.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
// We use gmail as our smtp client
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(
"Email", "password");
smtpClient.Send(message);
msg = "Successful<BR>";
string message1 = "Your Query has been Submited Successfully";
}
catch (Exception ex)
{
}
return msg;
}
try port replace to 587 Also.

Related

Operation has timed out error on ASP.NET

I'm getting an error while sending email through asp.net SMTP server. It says operation has timed out. How to overcome the exception?
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("mymailid#hotmail.com");
mail.To.Add("yourmailid#gmail.com");
mail.Subject = "Verify your account";
mail.Body = "Your OTP for your account verification is "+OTP.ToString()+". Enter your OTP in verification window.";
mail.IsBodyHtml = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Timeout = 20000;
SmtpServer.Send(mail);
errmsgShow.Text = "Mail sent";
}
catch (Exception e)
{
//string errMsg = "alert('OTP sending failed. Error: " +e.ToString()+ "')";
//ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", errMsg, true);
//Console.WriteLine(e.Message.ToString());
errmsgShow.Text = e.ToString();
}
I have also included below in the web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.live.com" port="25" userName="mymailid#hotmail.com" password="password" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
Try removing SmtpServer.Timeout = 20000;.
Update:
i see you have SmtpClient SmtpServer = new SmtpClient();.
SmtpServer is a property of SmtpMail SmtpMail.SmtpServer. Try changing the variable name, something like
SmtpClient mailClient = new SmtpClient();

Error while sending mail - The SMTP server requires a secure connection or the client was not authenticated

i have tried mostly every example available on net but still no working,whenever i click on submit button it shows this error ,please anyone help me with this
protected void btnsubmit_Click(object sender, ImageClickEventArgs e)
{
DAL.s_email = txtforget.Text;
DataTable dt = new DataTable();
dt = BAL.forgot_pass(DAL);
if (dt.Rows.Count > 0)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("my#email.com");
msg.To.Add(txtforget.Text);
msg.Subject = "Yor Password details";
msg.Body = "Hi,<br>Please check your Login Details<br/><br/> Your Username : " + dt.Rows[0]["Username"] + "<br/><br/> Your Password " + dt.Rows[0]["Password"] + "<br/><br/>";
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("my#email.com", "****");
smtp.Send(msg);
Label2.Text = "your username and password is sent";
txtforget.Text = "";
}
else
{
Label2.Text = "Email is not registered";
}
}
web config
<system.net>
<mailSettings>
<smtp from="my#email.com">
<network host="smtp.gmail.com" password="****" port="587" userName="my#email.com" defaultCredentials="false" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
Try this
protected void btnsubmit_Click(object sender, ImageClickEventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add(toemail);
mail.From = new MailAddress("my#email.com");
mail.Subject = "Mail";
mail.Body = " Hi,<br>Please check your Login Details<br/><br/> Your Username : " + dt.Rows[0]["Username"] + "<br/><br/> Your Password " + dt.Rows[0]["Password"] + "<br/><br/>";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("my#email.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
I know this question is old but I wasn't able to find anything helpful in other posts. Be sure to check that your msg.From email is correct:
msg.From = new MailAddress("my#email.com");
Your mail server may successfully authenticate your credentials but reject your from email. To help explain what I'm talking about, here is a possible mail server's response:
--> 235 2.7.0 Authentication successful
Authenticated as credentials#email.com
<-- MAIL FROM:<from#email.com>
530 5.7.0 Authentication required
Connection closed
SMTP session terminated (Bytes in/out: 482/955)

How to send a email from C#.net? [duplicate]

This question already has answers here:
Sending email in .NET through Gmail
(26 answers)
Closed 9 years ago.
I am working on framework 2.I want to send email through my portal.I visited many site.From that I able to understand this much.Here is my code.I have created page design.What is smtpserver.This code giving me error.
System.Web.HttpException: The server rejected the sender address
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = txtFrom.Text;
// Recipient e-mail address.
Msg.To = txtTo.Text;
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(Msg);
Msg = null;
if you want to send mail using gmail account than use this code
sing System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from_add#gmail.com", "name");
var toAddress = new MailAddress("to_add#example.com", "name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Full soruce : How to Send Mails from your GMAIL Account through VB.NET or C#. Windows Programming, with a Bit of Customization
try this bellow code
Using System.Net.Mail;
public void SendEmail(string from, string to, string bcc, string cc, string subject,
string body)
{
try
{
MailMessage NewEmail = new MailMessage();
NewEmail.From = new MailAddress(from);
NewEmail.To.Add(new MailAddress(to));
if (!String.IsNullOrEmpty(bcc))
{
NewEmail.Bcc.Add(new MailAddress(bcc));
}
if (!String.IsNullOrEmpty(cc))
{
NewEmail.CC.Add(new MailAddress(cc));
}
NewEmail.Subject = subject;
NewEmail.Body = body;
NewEmail.IsBodyHtml = true;
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(NewEmail);
this.Label1.Text = "Email sent successful.";
}
catch
{
this.Label1.Text = "Email sent failed";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string from = "sender address";// like username#server.com
string to = TextBox1.Text; //Recipient address.
string bcc = "";
string cc = "";
string subject = "text";
string body = "text";
SendEmail(from, to, bcc, cc, subject, body);
}
Web.Config:
<system.net>
<mailSettings>
<smtp>
<network host="your stmp server" port="25" userName="your from email" password="your password"/>
</smtp>
</mailSettings>
</system.net>
Pass value as like below in sendMail method
sMailFrom=your Mail Id
sMailTo=To Mail Id
sSubj=Subject of Mail
sContent= Content of ur mail
bHTMLFormat=true
public static string sendMail(string sMailFrom, string sMailTo,
string sSubj, string sContent, bool bHTMLFormat)
{
try
{
System.Net.Mail.MailMessage mmsg = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
//System.Web.Mail.MailMessage mmsg = new MailMessage();
string smtpServer = ConfigurationSettings.AppSettings.Get("smtphost").ToString();
mmsg.From = new System.Net.Mail.MailAddress(sMailFrom);
mmsg.To.Add(sMailTo);
mmsg.Subject = sSubj;
mmsg.Body = sContent;
//mmsg.BodyFormat = (bHTMLFormat ? MailFormat.Html : MailFormat.Text);
mmsg.IsBodyHtml = bHTMLFormat;
smtpClient.Host = smtpServer;
if (ConfigurationSettings.AppSettings.Get("smtpport").ToString() != "")
smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings.Get("smtpport").ToString());
smtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential mailCredential = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings.Get("EmailID").ToString(), ConfigurationSettings.AppSettings.Get("Password").ToString());
smtpClient.Credentials = mailCredential;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Send(mmsg);
return "";
}
catch (Exception err)
{
return err.Message.ToString();
}
}

How can i send a comment in email?

I have some text boxes like name,email address,phone no. and comment on my page.
I have to send the values to my email address..
How should I do this??
I am doing:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("xyz#gmail.com");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = txtComment.Text;
SmtpClient.Send(message);
Response.Write("Email successfully sent.");
}
catch (Exception ex)
{
Response.Write("Send Email Failed." + ex.Message);
}
}
and I am getting the following error:
An object reference is required for the nonstatic field, method, or property 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)'
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
~~~~~~~~~~~~~~~~~~~~
SmtpClient.Send(message);
These lines are attempting to use members of the class SmtpClient. However, as these members are not defined as static, you need to refer to your instance of that class, which you have called client.
Try
client.Host = "localhost";
client.Port = 25;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
Also, have a read of this article on the differences between class and instance members.
Finally, as SmtpClient implements IDisposable, I would change your code to wrap it in a using block, as this will ensure you are correctly cleaning up your SMTP session after you are finished with it.
using (SmtpClient client = new SmtpClient())
{
// YOUR CODE
}
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
if (to == "")
to = fromAddress.Address;
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = fromAddress;
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = fromAddress;
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, "XXXX");//"XXXX" is the password of the sender.
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}
i had done this:-
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage mailmsg = new MailMessage();
string name = txtName.Text.ToString();
string contact = txtPhoneNo.Text.ToString();
string comment = txtComment.Text.ToString();
string checkIn = txtCheckIn.Text.ToString();
string from = txtEmail.Text.ToString();
mailmsg.To.Add("recipientemailid#gmail.com");
mailmsg.From = new MailAddress(from.Trim());
mailmsg.Subject = "Test message";
mailmsg.Body = "This Query is submitted by user " + name.Trim() + ", "+ contact.Trim() + ", " + comment.Trim() + ", " + checkIn.Trim();
mailmsg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential("recipientemailid#Gmail.com", "password");
client.Credentials = credentials;
try
{
//try to send the mail message
client.Send(mailmsg);
Response.Write("sent!");
}
catch
{
//some feedback if it does not work
Response.Write("failed!");
}
}
Try
client.Host = "localhost";
client.Port = 465;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
Do not try to use or manage disposable emails with this because you will be clearly exposed to spam-trappers. Your code is open and very easy to identify who is using the code and from where.

The SMTP server requires a secure connection or the client was not authenticated

in my forgot password page user enters email id and clicks on submit button,the submit click event sends a mail on his emailid,,now i m getting an error that smtp server requirs a secure connection or the client was not authenticated,,let me show my code
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
if (Page.IsValid)
{
PasswordRecovery1.MailDefinition.From = "victorzoya#gmail.com";
e.Message.IsBodyHtml = false;
e.Message.Subject = "Please read below to reset your password.";
e.Message.Body = e.Message.Body.Replace("<%email%>", PasswordRecovery1.UserName);
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["DSN"]);
SqlCommand userid = new SqlCommand("SELECT UserId from aspnet_Users WHERE UserName=#UserName", connection);
connection.Open();
userid.Parameters.Add("#UserName", SqlDbType.VarChar, 50);
userid.Parameters["#UserName"].Value = PasswordRecovery1.UserName;
SqlDataReader result = userid.ExecuteReader();
string UserId = "";
while (result.Read())
{
object obValue = result.GetValue(0);
UserId = obValue.ToString();
}
connection.Close();
string link = "http://www.fixpic.com/Passwordreset.aspx?userid=" + UserId;
e.Message.Body = e.Message.Body.Replace("<%resetlink%>", link);
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(e.Message);
e.Cancel = true;
}
}
in web.config i have defined the mail settings as
<mailSettings>
<smtp deliveryMethod="Network" from="sumitkumarruhela#gmail.com">
<network host="smtp.gmail.com" port="587" defaultCredentials="false" />
</smtp>
</mailSettings>
You need to be authenticated with GMail's SMTP:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#gmail.com");
mail.To.Add("youraccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
See followings:
http://learnlinq.blogspot.com/2009/04/smtp-server-requires-secure-connection.html
http://www.codeproject.com/KB/IP/SendMailUsingGmailAccount.aspx

Resources