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);
}
Related
I send a mail using WebApi. Mail sent successfully. I want to change from mail in it and I change it using below code but it take from mail as 'testweb#gmail.com'. I use testweb#gmail.com in webconfig and I want to set from as testfrom#gmail.com.
but not working as per below code and when I got mail it always from 'testweb#gmail.com' instead of 'testfrom#gmail.com'
Note : I use above emails only to ask question while development I use my real mail id.
Is there any other way to achieve this? Or I need to change anything.
Below is my code to send mail:
public static bool SendMail(string toAddress, string subject, string body)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("testfrom#gmail.com");
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
Below is webconfig smpt setting:
<mailSettings>
<smtp from="testweb#gmail.com">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
also try with below setting in webconfig:
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
You cant!!
You are using the google SMTP server, it doesn't allows to change the from address, if you want to change the from address try using some other smtp provider that allows so.
I am try to send email through SMTP in ASP.net but email not sending and not giving any error. My code:
//create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//set the FROM address
mail.From = new MailAddress("aaa#abc.com");
//set the RECIPIENTS
mail.To.Add("bbb#abc.com");
//enter a SUBJECT
mail.Subject = "Set the subject of the mail here.";
//Enter the message BODY
mail.Body = "Enter text for the e-mail here.";
//set the mail server (default should be smtp.1and1.com)
SmtpClient smtp = new SmtpClient("smtp.1and1.com");
//Enter your full e-mail address and password
smtp.Credentials = new NetworkCredential("example#abc.com", "xxxxxxx");
//send the message
smtp.Send(mail);
And my code in web.config is
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Mail\" />
</smtp>
</mailSettings>
<defaultProxy enabled="true" />
</system.net>
Use breakpoint to display each parameter's value. Even if there is no error in the codes, parameter's value may be null or wrong. Before breakpoint, use try-catch as it below;
try
{
// Your codes
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
Check First you TLS Version.
Open your Site in IE(Internet Explorer) browser and go to page properties.
If your TLS 1.2 then Add below code before client.Send(Object);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
like as per bellow
System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient();
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Send(oMail);
client.Dispose();
client = null;
First of all, I hope to be at the right place, I apologize in advance if this is not the case, please re-orient myself.
So here I have a worry, I have a website www.artipik.com with two forms, online quote and contact form.
My website is hosted by godaddy and servers were updated summers there for 15 days and my form are no longer work, which put me in a very problematic position. I'm a graphic designer not a coder, hence my difficulty changed the code.
I had the godaddy technical service and he gave it to me I think part of the solution. I found the place to change the code, but I can not seem to fit, I'm testing, I no further error messages but I do not receive any email I send.
My current ASP code is as follows:
<%
'-----version CDONTS-----
Set Mail = Server.CreateObject("CDONTS.NewMail")
Mail.BodyFormat = 1 '0:html/1:plain text
Mail.MailFormat = 1 '0:MIME/1:plain text
Mail.From = envoyeur
Mail.To = receveur
if copie<>vbnullstring then Mail.Bcc = copie
if sujet=vbnullstring then sujet="Formulaire de contact Artipik.com"
Mail.Subject = sujet
Mail.Body = message
Mail.Send
set Mail=Nothing
'response.cookies("email")=envoyeur
%>
and the new code that I have to adapt is the following (Using CDOSYS to Send Email from Your Windows Hosting Account):
// language -- C#
// import namespace
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thank you for your help.
Julian
System.Web.Mail.MailMessage is obsolete. You should use System.Net.Mail.MailMessage instead. It works almost exactly the same (warning! untested):
private void SendEmail()
{
using (var message = new MailMessage())
{
message.From = new MailAddress("emailaddress#domainname");
var toAddress = new MailAddress("emailaddress#domainname");
message.To.Add(toAddress);
message.Subject = "Test email subject";
message.Body = "Sent at: " + DateTime.Now;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
}
Add this to your web.config:
<configuration>
<system.net>
<mailSettings>
<smtp from="emailaddress#domainname">
<network host="your-smtp-host-address" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Recently I've had the same problem, could solve it throug the next way:
in web.config:
<system.net>
<mailSettings>
<smtp from="contacto#mydomain.com">
<network host="relay-hosting.secureserver.net" password="MyPass" port="25" enableSsl="false" defaultCredentials="false" userName="contacto#mydomain.com"/>
</smtp>
</mailSettings>
</system.net>
in codebehind:
MailMessage m = new MailMessage();
m.IsBodyHtml = true;
m.Body = "My Message";
m.To.Add("jhon.doe#yahoo.com");
m.Subject = "Go Daddy Emails Go Live!";
SmtpClient smtp = new SmtpClient();
smtp.Send(m);
I have a registration page I am sending an email from asp.net ,one mail to the registered user and other mail to the admin that too with different body...Ho can I achieve this? Also, User should give a confirmation link saying that you have subscribed and when the user clicks on the mail given link , a mail should fire to the admin saying that This User has been Registered.
http://www.webcodeexpert.com/2013/08/create-registration-form-and-send.html
You can use this link to achive the activation for user. But for sending mail to the admin, Use simple send email method.
Try this way, Send the from address and body details by yourself .
public void SendMail(string FromEmail, string Subject, string Body)
{
string ToEmail="YourAdmin#Gmail.com";
System.Net.Mail.MailMessage eMail = new System.Net.Mail.MailMessage();
eMail.From = new System.Net.Mail.MailAddress(FromEmail);
eMail.To.Add(ToEmail);
eMail.Subject = Subject;
eMail.IsBodyHtml = true;
eMail.Body = Body;
System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient();
SMTP.Send(eMail);
eMail.Dispose();
}
Web.Config:
<system.net>
<mailSettings>
<smtp>
<network host="your stmp server" port="25" userName="your from email" password="your password"/>
</smtp>
</mailSettings>
</system.net>
Should be you have mail hosting server .
and if you want to send mail without page postback , See my little bit blog
These lines of code is used to send mail in aspx.cs page,make a try.if yo are using asp.net with c#
in aspx
<asp:CheckBox ID="CheckBoxMark1" runat="server" />
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
//other code
}
public void sendMail(String toemail)
{
MailMessage mail = new MailMessage();
mail.To.Add(toemail);
mail.From = new MailAddress("sender#gmail.com");
mail.Subject = "Subject";
mail.Body = " Your Contents in the mail";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("sender#gmail.com", "Password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
I got Message that mail sent. But in Inbox there is no new email i have found that email in
server inetpub-> mailroot-> Queue
Please tell me the solution for this
Edited
public string btnSendmail()
{
// 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("myemail#itaxsmart.com","");
smtpClient.Host = "localhost";
smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("mailsendtol#itaxsmart.com");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = "Hello World" ;
smtpClient.Send(message);
return "Email successfully sent.";
}
catch (Exception ex)
{ return "Send Email Failed." + ex.Message;
}
Is your SMTP started? If not, start it. If it is already started, try restarting it.
Also turn on logging and see if that helps.
The moment asp.net manages to sent the message to the smtp server, you will get a success message. This only means that the message has reached your smtp server.
asp.net cannot control what your smtp server does with it.
Try using a public smtp server like gmail, if your local smtp server is giving trouble.
Add This code to your web.config
<system.net>
<mailSettings>
<smtp>
<network host="relay-hosting.secureserver.net"/>
</smtp>
</mailSettings>
</system.net>
and Remove This
smtpClient.Host = "localhost";
smtpClient.Port = 25;
Thanks,
Subhankar