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();
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 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.