i'm using MailMessage class to send emails
MailMessage msg = new MailMessage(fromAddr, toAddr);
when i create the new MailMessage object it automatically gets the host using the fromAddr.for an example,if my fromaddress is chamara#pindoc.com.au it assumes the host as pindoc.com.au but i have a different name for the host.so the host name is wrong.i think because of that i'm getting the following error.
{"Mailbox unavailable. The server response was: 5.7.1 Unable to relay"} System.Exception {System.Net.Mail.SmtpFailedRecipientException}
how can i solve this?
Have you checked your mailSettings? Example web.config below:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="no-reply#yourdomain.com">
<network defaultCredentials="true" host="mail.yourdomain.com" port="25"/>
</smtp>
</mailSettings>
</system.net>
you can specify the mail server when you create an instance of the SmtpClient object (as well as other details like port numbers and authentication)
SmtpClient client = new SmtpClient("different.hostname"); // specify your hostname
client.Send(msg);
You could also specify your smtp details in the web.config or app.config and the SmtpClient will pick these up automatically...
SmtpClient client = new SmtpClient();
client.Send(msg);
Typically, i'll use SmtpClient to send messages. It's constructor takes a host and port:
SmtpClient mailClient = new SmtpClient("mail.domain.com", 25);
mailClient.Send(msg);
Related
I have a contact us page set up on my website and I would like to make it appear like it is coming from a different email address.
<mailSettings>
<smtp from="info#magazine.com">
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="admin#magazine.com" password="password" defaultCredentials="false" />
</smtp>
</mailSettings>
So I make my mail client and try and send the email
MailMessage message = new MailMessage();
message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactEmailTo"]));
message.Subject = "Contact Request";
message.Body = body;
SmtpClient client = new SmtpClient();
client.Send(message);
However, when I receive the email, I receive it from the admin address.
Use one of the constructor overloads on MailAddress to specify an address and display name like so:
MailAddress address = new MailAddress("user#website.com", "John Smith");
See: http://msdn.microsoft.com/en-us/library/1s17zfkf%28v=vs.110%29.aspx for more info.
It looks like you're sending through GMail. Their SMTP server rewrites the FROM address. See for example this question.
If you want to use GMail, this answer suggests adding the FROM email in your account settings under:
Settings -> Accounts -> Send mail as -> Add another email address you own
The other option is to use a different SMTP server that allows you to set your FROM address.
C# I am trying to send mail with following code. OS is windows 2008 server. SMTP is installed.
MailMessage message = new MailMessage();
string[] recipientslist = recipients.Split(';');
foreach (string recipient in recipientslist)
{
if (!String.IsNullOrEmpty(recipient))
{
System.Net.Mail.MailAddress mailAddress =
new System.Net.Mail.MailAddress(recipient);
message.To.Add(mailAddress.Address);
}
}
message.From = new MailAddress(sender);
message.Subject = subject;
message.Body = body;
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(message);
In web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="PickupDirectoryFromIis">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Even smtp is installed,
I m getting Exception: Failure sending mail.
Stack Trace: at System.Net.Mail.SmtpClient.Send(MailMessage message)....
==========================================================================================
The other scenario when SMTP is not installed, it giving me the exception Cannot get IIS pickup directory
Any help is highly appreciable.
Thanks*strong text*
I got the answer..
Permission is needed to default IIS pickup directory. I just added permissions for Network service and done.
Following is the check list that should be verified before sending mail..
http://torontoprogrammer.ca/2011/04/fixing-the-cannot-get-iis-pickup-directory-error-in-asp-net/
Thanks
This is an anti-virus program, QH Exchange Protection, interfering with sending mail from the application. Contact your system administrator about this. If you are the administrator of your system, you'll need to find out how to tell Quick Heal's product not to do this.
I've recently been having issues sending email from my web application. I keep getting a connection refused exception from the mail relay (and it's always the same mail relay). After some thorough discussion with the mail team, I've been told that I'm not using the MX record to sent the mail. However, I think I am. The MX-record is mailhub-us.xxx.us.net. Here is the code that I use to send emails (clearly I reference the mailhub address as the server)
MailMessage msgMail = new MailMessage();
****Some code to populate msgMail
SmtpClient smtpClient = new SmtpClient("mailhub-us.xxx.us.net");
smtpClient.Send(msgMail);
Yes, I'm aware I'd be better off using <mailsettings> in the web.config (something that I learnt during my research and something I intend to correct). I've checked to ensure that the MX records are set up at the DNS using nslookup and there are 3 servers configured for this entry.
I'm a little confused at this point, because I thought I was using the MX record and hence the failovers should automatically take place. Am I being stupid in saying that or is there something else that I'm missing? Any help in this issue is appreciated.
In your web.config file add this
<system.net>
<mailSettings>
<smtp from="fromemail">
<network host="hostname" defaultCredentials="false"
port="xx" userName="xxxx" password="xxx" />
</smtp>
</mailSettings>
and in your backend code, you could do something like
var message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(from);
message.To.Add(to);
message.Subject = subject;
message.Body = msg;
var client = new SmtpClient();
client.Send(message);
I have the following problem:
Using asp.net C# and calling several places in code to send email to the user.
However the from address in different sections of the code is different, and I have only one email address configured in web.config. This causes email sent from non-configured email addresses to go to users' junk box, how do I prevent this.
I have the following in web.config. So if from any place in the code the from address i not hello123#site.com, it will go to users' junk box.
<smtp deliveryMethod="Network">
<network
host="smtp.site.com"
userName="hello123#site.com"
password="mypassword"
/>
</smtp>
Here is c# code:
MailMessage message = new MailMessage();
message.From = new MailAddress("forgotyourpassword#abc");
message.To.Add(new MailAddress(this.txt_Email_Pass.Text));
message.Subject = "Welcome to abc";
message.Body ="abc";
SmtpClient client = new SmtpClient();
client.Send(message);
Unless your code is not setting the From address properly (please post the code). That has to do with your SMTP provider.
I'm updating some existing code that sends a simple email using .Net's SMTP classes. Sample code is below. The SMTP host is google and login info is contained in the App.config as shown below (obviously not real login info :)).
The problem I'm having, and I haven't been able to find any answers Googling, is that I can NOT override the display of the "from" email address that's contained in the "username" attribute off the Network element in the config in the delivered email.
In the line below that explicitly sets the From property off the myMailMessage object, that value, "Sparky#myDomain.com" does NOT display when the email is received. It still shows as "erroruser#myDomain.com" from the Network tag. However, the From name "Sparky" does appear in the email.
I've tried adding a custom "From" header to the Header property of the myMailMessage but that didn't work either.
Is there anyway to login to the smtp server, as shown below using the Network tag credentials, but in the actual email received override the From email address that's displayed?
Sample code:
MailMessage myMailMessage = new MailMessage();
myMailMessage.Subject = "My New Mail";
myMailMessage.Body = "This is my test mail to check";
myMailMessage.From = new MailAddress("Sparky#MyDomain.com", "Sparky");
myMailMessage.To.Add(new MailAddress("receiver#MyDomain.com", "receiver name"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMailMessage);
in App.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="errors#mydomain.com">
<network host="smtp.gmail.com" port="587" userName="erroruser#mydomain.com" password="mypassword" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
This is dependent on the SMTP server. Google's SMTP server does not allow you to create your own "from" field, but I have found other SMTP servers that will. Is it necessary that you use Google's server?