Reputation: 539
I am configuring SMTPClient with these codes to use local directory:
EmailHelper.cs
public bool SendMail(string from, string to, string cc, string subject, string body, bool isBodyHtml)
{
try
{
var smtpClient = new SmtpClient();
string pickUpFolder = @"C:\Users\kerem\Documents\Visual Studio 2012\Projects\Blog\Blog\Email";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
Configuration configurationFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
pickUpFolder = mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
smtpClient.PickupDirectoryLocation = pickUpFolder;
}
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(new MailAddress(to));
if (cc != "")
mailMessage.CC.Add(new MailAddress(cc));
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
return true;
}
catch (Exception err)
{
return false;
}
}
EmailHelper.cs in BlogServices Project in my Blog solution. Also there is a Blog Project in the same solution. I have replaced Blog\Blog\Email
in pickUpFolder
with Blog\Email
and Blog\BlogServices\Email
but I still have an error of Only absolute directories are allowed for pickup directory. Where is my mistake? Thanks in advance.
Upvotes: 1
Views: 6267
Reputation: 1579
Add this to your project's web.config.
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\kerem\Documents\Visual Studio 2012\Projects\Blog\Blog\Email\"/>
</smtp>
</mailSettings>
</system.net>
Alternatively, try deleting this line: pickUpFolder = mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
, but I suspect you'll have other problems if you go that route.
Upvotes: 3