ASP.NET MVC smtp error sending mail problem - asp.net

I have already edited the webconfig file but I've got SmtpException "error sending mail". My domain port is 25. I've double checked. Here is my code. What's problem? Thank you.
public void SendEmail(string toAddress, string fromAddress,string subject, string message)
{
try
{
using (var mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(new MailAddress(toAddress));
mail.Subject = subject;
mail.Body = message;
mail.IsBodyHtml = true;
try
{
using (var smtpClient = new SmtpClient())
{
smtpClient.Host = "webmail.domain.com";
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential("blabla#domain.com", "pass");
smtpClient.Send(mail);
}
}
mail.Dispose();
}
}

Related

Email Is not sending successfully from Asp.net application

I am trying to send Email from My asp.net web application which i have developed only for practice purpose but it is not working at all i tried so many things, so many times from google and youtube also but it is not working at all. please do help me if possible.....
one more thing i am doing it in college campus and their firewall is on so i tried it from another network also still it is not working...???
public bool sendMail(string from, string to, string subject, string body)
{
bool flag = false;
var message = new MailMessage();
try
{
message.To.Clear();
message.To.Add(new MailAddress(to));
message.From = new MailAddress(from);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "Email#mail.com",
Password = "password"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(message);
}
flag = true;
}
catch (Exception ex)
{
}
return flag;
}

Getting Error- Invalid mail Attachment

I am trying to send email with excel file attachment. But getting invalid mail attachment error.
Please help me.
Here is my code-
Here is code for Email sending-
protected void btnSend_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress(txtFrom.Text);
msg.To.Add(txtTo.Text);
msg.Subject = txtSubject.Text;
if (FileUpload1.HasFile)
{
String FileName = FileUpload1.PostedFile.FileName;
MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);
// msg.Attachments.Add(mailAttachment);
msg.Attachments.Add(mailAttachment);
}
using (SmtpClient client = new SmtpClient())
{
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(msg);
}
}
Right click on project name in solution explorer . Click on Add then Choose new folder name it MailDocuments. Use this code to Save it and Attach It.
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.PostedFile.FileName;
string Path = Server.MapPath("~/MailDocuments/");
FileUpload1.PostedFile.SaveAs(Path + fileName);
MailAttachment ma = new MailAttachment(Server.MapPath("~/MailDocuments/" + fileName), MailEncoding.Base64);
msg.Attachment.Add(ma);
}
private bool SendEmail(string myIPAddress)
{
bool emailsent = true;
if (myemailaddress != "" && mypassword != "")
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(myemailaddress);
mail.CC.Add("whoever#yahoo.com");
mail.From = new MailAddress("do.not.reply#whatever.com", "do.not.reply#whatever.com", System.Text.Encoding.UTF8);
mail.Subject = "Type Subject Here";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "Your IP address has changed to " + myIPAddress;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("yourGmailEmail#gmail.com", "thepasswordforthataccount");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
emailsent = false;
}
}
else
{
emailsent = false;
}
return emailsent;
}

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.

exception while sending mail

I am sending mail but it is giving me exception
Mailbox name not allowed. The server response was: sorry, relaying denied from your location [182.72.17.210] (#5.7.1)
my code
public static string SendMailHtmlFromat(string sForm, string sTo, string sSub, string sMsg)
{
MailMessage mailmsg = new MailMessage();
mailmsg.To.Add(sTo);
mailmsg.From = new MailAddress(sForm);
mailmsg.Subject = sSub;
mailmsg.IsBodyHtml = true;
mailmsg.Body = sMsg;
try
{
//SmtpClient smtp = new SmtpClient("smtpout.secureserver.net", 25);
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
smtp.Credentials = new System.Net.NetworkCredential("admin#beautiful.com", "Smart123");
smtp.Send(mailmsg);
}
catch (Exception e)
{ }
return "";
}
is their any problem in SmtpClient declaring
Check the below link.
http://www.eggheadcafe.com/community/aspnet/2/10227041/exception-while-sending-mail.aspx

Resources