Mail Configuration is Nlog.config file
<target name="mail" xsi:type="Mail" smtpServer="smtp.emailsrvr.com" smtpPort="25" smtpUserName="samplemail#samplemail.com" smtpPassword="Password"
from="sample-test#intsof.com" to="vinay#sample.com" subject="Hello Mail from Nlog" html="false" encoding="UTF8"/>
<rules>
<logger name="*" level="Error" writeTo="mail"/>
Configuration in App.config.
<configuration>
<system.net>
<mailSettings>
<smtp from="Sample-test#sample.com" deliveryMethod="Network">
<network defaultCredentials="true" userName="vinay-test#sample.com" password="Password" host="smtp.emailsrvr.com" port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
If i use the delivery method SpecifiedPickupDirectory and give a local path then its sending mail, but not to outlook.
For this question, the accepted answer recommended changing encoding="UTF8" to encoding="UTF-8". Try that and see if it helps.
This can be a solution as well:
Config:
from="${event-context:item=Sender}"
Logging code:
eventInfo.Properties.Add("Sender", UserPrincipal.Current.EmailAddress);
Related
I'm using NLog, and I'm pretty new at logging. I created the log files but somehow I have a problem whit sending e-mails. I followed all instructions but couldn't make it.
Mail settings in configuration tags in web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory" from="some#some.org">
<network host="localhost"/>
<specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
</smtp>
</mailSettings>
</system.net>
And this is the target inside the nlog.config:
<target name="Mail" xsi:type="Mail" html="true" subject="Error Received"
body="${message}"
to="some1#some.com"
from="some#gmail.com"
encoding="UTF-8"
smtpUserName="some#some.com"
enableSsl="false"
smtpPassword="password"
smtpAuthentication="Basic"
smtpServer="smtp.some.com"
smtpPort="25" />
The rule I used:
<logger name="*" minlevel="Error" writeTo="Mail" />
And I called the logger like this:
Logger logger = LogManager.GetCurrentClassLogger();
try{ //something }
catch(Exception ex){ logger.Error(ex); }
And also I'm pretty confused about the places of the settings and configurations. Thank you.
The body is a layout format, not sure if it will process the ${message} tag. Change ${message] to a layout name that is in nlog.config or leave it off for the ${message}${newline} default.
You can turn on the Internal Debugging
Change the top XML parent to
<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">
This might give you an idea on the issue. Could be an authentication issue with the server or another issue.
Since you are specifying all the server information in the target, you don't need the web.config settings.
Not sure if it is fixed yet, but you should add a timeout="10000" to the target so it closes the connection.
There is a pretty good example in the NLog Wiki for GMail that works
I am facing an issue with getting a value from Web.config.
Here is my web.config code which contains Key
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Email" value="myname#mydomain.com" />
i want email key value, i am writing,
string From = ConfigurationManager.AppSettings["Email"].ToString();
but its giving me error "Object reference is not set to an instance of an object"
My other web.config declarations are:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="***" enableSsl="true" defaultCredentials="false" userName="myname#mydomain.com" password="mypassword" />
</smtp>
</mailSettings>
</system.net>
Any help appreciated!
Thanks!
Make sure that your start up project is set correctly. If you're running this from a separate project (i.e. in a test), it won't be looking at that Web.config but its own Web/App.config file.
I have code for sending email to clients.
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
MailMessage Message = new MailMessage("From", "To", "Subject", "Body");
Client.Send(Message);
With following in App.config.
<system.net>
<mailSettings>
<smtp from="support#MyDomain1.com">
<network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" />
</smtp>
</mailSettings>
<mailSettings>
<smtp from="support#MyDomain2.com">
<network host="smtp.MyDomain2.com" port="222" userName="xyz" password="helloPassword2" />
</smtp>
</mailSettings>
</system.net>
Problem is that in my code how can I differentiate which mailsetting to use for a given mail, meaning when I want to send mail from MyDomain1 account vs MyDomain2 account
There's one thread that contains the explanation how to achieve this.
Setting multiple SMTP settings in web.config?
Wanted originally to post it as an answer, but the system automatically converted it into a comment. Seems I have to write longer messages.
Glad it helped :)
I have following config for my mail:
<system.net>
<mailSettings>
<smtp from="foo#bar.com" deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:/test"/>
<network host="localhost" userName="" password=""/>
</smtp>
</mailSettings>
</system.net>
This is my .Release version:
<system.net>
<mailSettings>
<smtp from="foo#bar.com" xdt:Transform="RemoveAttributes(deliveryMethod)">
<network xdt:Transform="Replace" host="192.168.1.9" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
How do I remove
<specifiedPickupDirectory pickupDirectoryLocation="C:/test"/>
so it doesn't show in my .Release at all?
Also, I would like to remove other namespaces like System.Diagnostics completely. What is the syntax for doing so?
For specifiedPickupDirectory element this should work:
<specifiedPickupDirectory xdt:Transform="RemoveAll" />.
For System.Diagnostics:
<system.diagnostics xdt:Transform="RemoveAll"></system.diagnostics>
<system.net>
<mailSettings>
<smtp from="foo#bar.com" xdt:Transform="Replace">
<network xdt:Transform="Replace" host="192.168.1.9" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
This will replace that entire tag with yours.. hope this is what you are looking for..
the good thing about this is that you dnt end up polluting your transform config with unnecessary remove commands like some of the answers stated here..
consider the case where you have more than one child tags..
Rather than attempting to remove the config from your release version, can you take it from the base version and just add it to the .Debug version? That might be simpler. However if you want to remove it I think you can use <specifiedPickupDirectory xdt:Transform="Remove"/> or something similar.
#katit , you should maintain two different configs for dev and release.
your webconfig should be dynamic and take the configs as below
/qa/yoursettings.config
/release/yoursettings.config
one more sample
<connectionStrings configSource="config\qa\connectionStrings.config"/>
when you go to qa or release , switch your web.config accordingly.
in this way it will be lot cleaner.
I am using SmtpClient to send an email with an attachment.
However for a certain batch we need to somehow save the MailMessage instead of sending them.
We are then thinking/hoping to manually upload the messages to the users drafts folder.
Is it possible to save these messages with the attachment intact (impossible, I would have thought). Or alternatively upload the messages to a folder in the users account?
If anyone has any experience of this, I'd much appreciate a bit of help or a pointer.
When testing in ASP.NET we save our emails to a folder rather then send them through an email server. Maybe you could change yourweb.config settings like this for your batch?
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
</smtp>
</mailSettings>
</system.net>
Additional Info:
MSDN: <specifiedPickupDirectory> Element (Network Settings)
Configuring SmtpClient to drop emails in a folder on disk
As well as the SpecifiedPickupDirectory information of the other answers, if you want to ensure your emails are sent to a folder relative to the site root - handy in testing on build servers where you don't know the paths - you can add a quick check in your email sending code:
SmtpClient client = new SmtpClient();
...
// Add "~" support for pickupdirectories.
if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
pickupRoot = pickupRoot.Replace("/",#"\");
client.PickupDirectoryLocation = pickupRoot;
}
And your tests will look something like this (make sure you use App_Data so IIS can write to the folder):
// Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
if (!Directory.Exists(pickupPath))
Directory.CreateDirectory(pickupPath);
foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
{
File.Delete(file);
}
// Act (send some emails)
// Assert
Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));
This can help - Adding Save() functionality to Microsoft.Net.Mail.MailMessage
The main ideia, make an extension to MailMessage ,that by reflection making a save method.
You can configure this with the system.net setting in your web.config / app.config file.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
<!-- Use this setting for development
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
</smtp>
-->
</mailSettings>
</system.net>
Also, here's a link with info on migrating from System.Web.Mail to System.Net.Mail.
A bug also requires adding as a workaround in some versions of the framework. So the completed version looks like:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>