I am having a problem to send out a test email using "system.net.mail". The error message " Mailbox name not allowed. The server response was: 5.3.0 Relaying denied check mail first" every time I press the submit button. Thank you in advance.
Code_behind:
public class MailHelper
{
public static void SendMailMessage(string from, string to, string bcc,
string cc, string subject, string body)
{
MailMessage mMailMessage = new MailMessage();
mMailMessage.From = new MailAddress(from);
mMailMessage.To.Add(new MailAddress(to));
if ((bcc != null) && (bcc != string.Empty))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
if ((cc != null) && (cc != string.Empty))
{
mMailMessage.CC.Add(new MailAddress(cc));
}
mMailMessage.Subject = subject;
mMailMessage.Body = body;
mMailMessage.IsBodyHtml = true;
mMailMessage.Priority = MailPriority.Normal;
string smtpServer = "";
//Read the Web.config to get configuration setting
smtpServer=System.Web.Configuration.WebConfigurationManager.AppSettings["smtp"];
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient(smtpServer);
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
}
Web_config:
<appSettings>
<add key="smtp" value="smtp.mydomain.com"/>
</appSettings>
<system.net>
<mailSettings>
<smtp from="defaultEmail#mydomain.com">
<network host="smtp.mydomain.com" port="25" userName="yourUserName" password="yourPassword"/>
</smtp>
</mailSettings>
</system.net>
Sumbit_Click:
protected void Sumbit_Click(object sender, EventArgs e)
{
MailHelper.SendMailMessage("from#email.com", "to#email.com", null, null,
"subject", "textEnquiry.Text");
}
Related
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.
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();
}
}
Is there any way in asp.net that when my page is open on web then an email alert with username should be generate so that i have information that which one is accessing my page
I changed the code like bellow
Public Sub SendMail(ByVal id As String)
Dim message = New MailMessage()
message.IsBodyHtml = True
message.From = New MailAddress("adeel.aslam0#gmail.com")
message.[To].Add(New MailAddress("malik.adeel#shakarganj.com"))
message.Subject = "user access page"
message.Body = "Your Message"
' add id here
Dim client = New SmtpClient("smtp.gmail.com")
client.Send(message)
End Sub
and in web.config like this
<system.net>
<mailSettings>
<smtp from="adeel.aslam0#gmail.com">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="adeel.aslam0#gmail.com" password="pass" />
</smtp>
</mailSettings>
</system.net>
You might do something similar..
Modify your web.config file to send emails
e.g. This use gmail settings
<system.net>
<mailSettings>
<smtp from="yourMailId#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="yourmail#gmail.com" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
And in your page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var user = HttpContext.Current.User;
if (user != null && user.Identity.IsAuthenticated)
{
var id = Membership.GetUser().ProviderUserKey;
SendMail(id.ToString());
}
}
}
public void SendMail(string id)
{
var message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("yourmail");
message.To.Add(new MailAddress("tomail"));
message.Subject= "user access page";
message.Body = "Your Message";// add id here
var client = new SmtpClient();
client.Send(message);
}
if you using vb try something like this.
This way, you will not need to change your web.config
Dim mailobject As New System.Net.Mail.MailMessage()
Dim myCred As New System.Net.NetworkCredential("yourmail#gmail.com", "password")
mailobject.To.Add("tomail")
mailobject.Subject = "User Access The Page"
mailobject.From = New System.Net.Mail.MailAddress("frommail")
mailobject.IsBodyHtml = True
mailobject.Body = "Your Message"
Dim SmtpMail As New System.Net.Mail.SmtpClient("smtp.gmail.com")
SmtpMail.UseDefaultCredentials = False
SmtpMail.EnableSsl = True
SmtpMail.Credentials = myCred
SmtpMail.Port = 587
SmtpMail.Send(mailobject)
Fair question. But you really do not want to do this unless you have a very limited amount of visitors. See the answer above on how to do it.
Better write everything into a table and send yourself a digest at certain interval per hour/day. This way you can also do some reporting afterwards using that table.
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
I have an ASP.NET application hosted on Godaddy that I want to send email from. When it runs, I get: Mailbox name not allowed. The server response was: sorry, relaying denied from your location. The important parts of the code and Web.config are below:
msg = new MailMessage("accounts#greektools.net", email);
msg.Subject = "GreekTools Registration";
msg.Body =
"You have been invited by your organization to register for the GreekTools recruitment application.<br/><br/>" +
url + "<br/><br/>" +
"Sincerely,<br/>" +
"The GreekTools Team";
msg.IsBodyHtml = true;
client = new SmtpClient();
client.Host = "relay-hosting.secureserver.net";
client.Send(msg);
<system.net>
<mailSettings>
<smtp from="accounts#greektools.net">
<network host="relay-hosting.secureserver.net" port="25" userName="********" password="*********" />
</smtp>
</mailSettings>
1- If your site is hosted on godaddy you may use "relay-hosting.secureserver.net" without credentials.
2- If your site is hosted outside of godaddy you may use "smtpout.secureserver.net" with you email account credentials.
PS: Please change port 3535 if you have problems with 25
Hosted On GoDaddy
<system.net>
<mailSettings>
<smtp from="abc#xyz.net">
<network host="relay-hosting.secureserver.net"/>
</smtp>
</mailSettings>
</system.net>
External
<system.net>
<mailSettings>
<smtp from="abc#xyz.net">
<network host="smtpout.secureserver.net"
userName="abc#xyz.net" password="your_password_here"
port="25" />
</smtp>
</mailSettings>
</system.net>
Here's my email class:
public class Email
{
public enum MailAddressType
{
From = 1,
Bcc
}
private static MailAddress _from = null;
public static void SendEmail(string to, string subject, string body)
{
SendEmail(to, subject, body, From, string.Empty);
}
public static void SendEmail(string to, string subject, string body, string from)
{
SendEmail(to, subject, body, from, MailAddressType.From);
}
public static void SendEmail(string to, string subject, string body, string addresses, MailAddressType addressType)
{
MailAddress from = From;
string bcc = string.Empty;
if (MailAddressType.From == addressType)
{
from = new MailAddress(addresses);
}
else
{
bcc = addresses;
}
SendEmail(to, subject, body, from, bcc);
}
private static void SendEmail(string to, string subject, string body, MailAddress from, string bcc)
{
MailMessage message = new MailMessage();
message.From = From;
message.To.Add(to);
if (!string.IsNullOrEmpty(bcc))
{
message.Bcc.Add(bcc);
}
message.ReplyTo = from;
message.Subject = subject;
message.Body = HttpContext.Current.Server.HtmlEncode(body);
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
public static MailAddress From
{
get
{
if (null == _from)
{
SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string address = section.From;
string displayName = ConfigurationManager.AppSettings["fromEmailDisplayName"];
_from = new MailAddress(address, displayName);
}
return _from;
}
}
}
And here are the related web.config settings:
<appSettings>
<add key="fromEmailDisplayName" value="Firstname Lastname"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="myname#mydomain.com">
<network host="relay-hosting.secureserver.net" />
</smtp>
</mailSettings>
</system.net>
For me, the key was "message.From = From" and "message.ReplyTo = from". GoDaddy seems to want the message to come from an address in your domain. So for contact pages, use your default email address as the From and set the sender as the ReplyTo. Email goes through fine after that.
This is likely a reply from the SMTP server because the machine attempting to send email has not been whitelisted (or is blacklisted for SPAM). Is relay-hosting.secureserver.net a GoDaddy server, or is it on a different network? You might want to find a GoDaddy server that enables email to be relayed. I would imagine a lot of shared hosting providers have restrictions today.
I would find out what type of SMTP server you are using and what anti-spam measures are in place. The administrator may be able to add the GoDaddy server to the whitelist of allowed hosts. You need to be very careful and make sure that you application cannot be used as a proxy for a spammer. Make sure to validate all input to ensure it it safe.
Check your hostname. Are you sure your account isn't configured to use mail.greektools.net ?That's the default format for GoDaddy webhosting..
set
defaultCredentials="false"
in your network element
<network host="relay-hosting.secureserver.net" port="25" userName="********" password="*********" defaultCredentials="false" />
What email or relay server should I use in my ASP.NET 3.5 code?
You do not need to provide a user name
and password for this relay server.
Just for a test. Remove the username and password values from the web.config.
Then, in your code set
//call this line, before you call .Send
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(msg)
I just asked GoDaddy, how to set up a SMTP form mailer, and they told me that I'd need to use a Relay Server, with no username, no password, and no port. The server name to use was, the same name that you used.
var message = new MailMessage();
message.To.Add(new MailAddress("email-address"));
message.From = new MailAddress("email-address");
message.Subject = "subject";
message.Body = string.Format("message-body");
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Host = "relay-hosting.secureserver.net";
smtp.EnableSsl = false;
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
await smtp.SendMailAsync(message);
}
For those who want to know what should be C# code in addition to the accepted answer, below code worked for me. Please note that "from" address is already mentioned in web.config in the accepted answer, so no need to mention it in C# code.
public static void SendMail(string emailid, string subject, string body)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(new MailAddress(emailid));
msg.Subject = subject;
msg.IsBodyHtml = true;
msg.Body = body;
client.Send(msg);
}
Try below code:
smtp.Host = "relay-hosting.secureserver.net";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("test#yourwebsitedomain.com", "*******");
It worked for me.
The code working for me for Go Daddy email send from c# code
var smtp = new SmtpClient
{
Host = "smtpout.secureserver.net",
Port = 25,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("domain.xyz.com", "password...")
};
using (var message = new MailMessage("domain.xyz.com", "domain.xyz.com")
{
IsBodyHtml = false,
Subject = modal.Subject,
Body = modal.Body
})
{
smtp.Send(message);
}