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
Related
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 have newly built a windows 2008 R2 server(with .NET 3.5) and added the following too
IIS, SMTP, Visual Web Developer 2010 Express, but no MS Office or Outlook.
and i started SMTP server using IIS 6.0 administration (through control panel -> Admin tools -> IIS 6.0..)
And using the below code...
using System.Net;
using System.Net.Mime;
using System.Net.Mail;
public static void SendMail(string From, string To, string Subject, string BodyText)
{
MailMessage mailMsg = new MailMessage();
mailMsg.Subject = Subject;
//from and To
mailMsg.From = new MailAddress(From);
mailMsg.To.Add(new MailAddress(To);
//Body Text
mailMsg.Body = BodyText.ToString();
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Send(mailMsg);
// Clean up.
mailMsg.Dispose();
}
//Web.Config entry - 255.255.255.255 is the server IP
<system.net>
<mailSettings>
<smtp from="myname#gmail.com">
<network host="255.255.255.255" port="25" userName="" password=""/>
</smtp>
</mailSettings>
</system.net>
When i use the above method to send email, it shows an error "Failure Sending Mail Error", but does not give details.
As many people suggested I have also added the IP address to SMTP Server (under properties -> general tab) still shows the same message. So i'm wondering is Outlook required for sending mails from asp.net.
Please suggest what else can i check to find the actual problem and make it work.
It would seem you are using the wrong settings, try these with the proper information Username Password Host etc
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("username", "password");
smtp.Host = "setting";
Hope this helps
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.