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.
Related
I have using office 365 smtp for sending email in my application. it is working and getting emails in my local system while debugging in visual studio. But when hosted to iis in server the email is not getting, no erroris showing.
here is my smtp settings in web config
<system.net>
<mailSettings>
<smtp from="myOffice365account">
<network host="smtp.office365.com" port="587" password="myPassword" userName="myOffice365account" defaultCredentials="false" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
and this is the code for sending email
public bool SendMail(string[] to, string subject, string body,Attachment attachement=null)
{
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.Default;
mail.From = new MailAddress(ConfigurationManager.AppSettings["Email_FromAddress"]);
if (attachement !=null)
{
mail.Attachments.Add(attachement);
}
mail.To.Add(string.Join(",", to));
SmtpClient mailClient = new SmtpClient();
mailClient.Send(mail);
return true;
}
after clicking send button in iis hosted application the success message is coming no error, but the mail not getting. The port 587 is already opened.
Is there anyother settings in server or code for this.
Thanks in advance.
I hard the same issue that my solution works locally but on the production server all the email sent were not delivery. I have to Event Viewer on the server ---> Windows Logs ---> Application, Then I send and email the log drop and I was able to see the error
I have implemented a server that sends emails via .Net SmtpClient.
the mail sending code looks like that:
private static MailMessage SendMail(string to, string subject, string body)
{
MailMessage mailToSend = new MailMessage();
mailToSend.Body = body;
mailToSend.Subject = subject;
mailToSend.IsBodyHtml = true;
mailToSend.To.Add(to);
try
{
mailClient.Send(mailToSend);
}
catch (Exception ex)
{
//Log data...
}
mailToSend.Dispose();
}
and in Web.config i've put the mail's credentials, someting like that:
<configuration>
<system.net>
<mailSettings>
<smtp from="autoemail#mailserver.org">
<network host="smtp.mailserver.org" password="pswdpswd" port="25" userName="autoemail" clientDomain="the-domain" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
The emails sent successfuly and everything works fine BUT when I'm logging in to the email user in the exchange server (in example via Outlook Web-App) I can't see the mail sent via SmtpClient (via code) in the sent items folder.
how can I keep a copy of the sent mails in this folders?
Thanks!
They are not recorded in the sent items since it is only send using the account from the user on SMTP level, it doesn't really use the mailbox to send the email.
The only option you have is not to use SmtpClient and use the Exchange API to send mail.
From their sample referenced:
ExchangeService service = new ExchangeService();
service.AutodiscoverUrl("youremailaddress#yourdomain.com");
EmailMessage message = new EmailMessage(service);
message.Subject = subjectTextbox.Text;
message.Body = bodyTextbox.Text;
message.ToRecipients.Add(recipientTextbox.Text);
message.Save();
message.SendAndSaveCopy();
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?