The question is too simple, but still appreciate the short answer. I would like the SmtpClient to fetch username/password from the App.config file. From the MSDN/schema I've figured out that the proper file (excerpt) should look like:
<system.net>
<mailSettings>
<smtp from="foo#bar.com">
<network
host="mail.bar.com"
port="25"
userName="foouser"
password="barpassword"
/>
</smtp>
</mailSettings>
</system.net>
I am trying to find the proper API to call, when initializing SmtpClient state, so that mail and password will be nicely fetched from the XML:
var client = new SmtpClient( ... ); // how to fetch the servername?
client.Credentials = new NetworkCredential( ... , ... ); // how to fetch user/pass
client.Send(message);
Is there a proper/dedicated way to fetch servername, user, password or should I just call the "regular" API like ConfigurationManager.AppSettings["server"]?
Nothing special is needed, just initialize and send :)
SmtpClient client = new SmtpClient();
client.Send(mymessagehere);
That is all, it will pull from the config.
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.
I understand this topic has been well elaborated previously, but I have looked around the Internet for various solutions and nothing has helped so far.
I'm making a web application which at one point is supposed to send an email to the email that the user provided. I'm trying to send it from my gmail account.
I tried the following:
public void test()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mygmail#gmail.com", "password*"),
EnableSsl = true
};
client.Send("mygmail#gmail.com", "mygmail#gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
And I'm getting the following error: The server response was: 5.5.1 Authentication Required.
In my web.config file I wrote
<system.net>
<mailSettings>
<smtp>
<network host="smtp.google.com" password="" userName=""/>
</smtp>
</mailSettings>
</system.net>
But that didn't work. I also tried adding the password* and the username (mygmail#gmail.com) in the web.config instead of the empty strings for userName and password, but I still got the same error.
Could anyone please help me out?
Thank you.
Try setting UseDefaultCredentials to false before specifying your custom credentials.
See c# SmtpClient class not able to send email using gmail
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);
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'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?