In my ASP.Net i have to save and open the SMTP mail.I am using SMTP for sending mail.At the same time i have to save it on "Folder" in the application and i want to open it how can i do that?
You need to instruct the SMTP client to save to a specific directory. The method below will save the MailMessage object as a .eml file.
MailMessage msg = new MailMessage();
msg.To.Add("[ToEmail]");
msg.From = new MailAddress("[FromEmail");
msg.Subject = "[YourSubject]";
SmtpClient client = new SmtpClient("[YourSmtpHost]");
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = #"C:\[YourDirectory]";
client.Send(msg);
Related
I am developing a program which needs to have the capability to send emails. I've got a simple mail function setup however I have never really delved into the mail side of things and am not sure if I'm using the correct settings.
I am sure I am doing something wrong with the SMTP, I have set the MailMessage host as the outgoing mail server that I use for emailing from outlook (the email accounts are hosted on shared virtual hosting so I use their supplied hostname in the function) alongside the login credentials I would normally use. When I try to send a test email it throws an unable to connect to remote server exception. I have WAMPSERVER setup on the computer I am trying to run this from, I know it has some kind of SMTP capability? Should I be using this or is there no reason I can't use shared virtual hosting SMTP as the host? Please refer to code below-
public void EmailTracking()
{
string to = "johnsmith#xxxxxxxxxxxxx.com.au";
string body = "this is some text for body";
string subject = "subject line";
string fromAddress = "kelvie#xxxxxxxxxx.com.au";
string fromDisplay = "Kelvie";
string credentialUser = "removed";
string credentialPassword = "removed";
string host = "removed";
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail); //fails here with unable to connect to remote server
}
Using System.Web.Mail in an old project I used to use the following code to build an authenticated message
MailMessage msg = new MailMessage();
// ... fill in to, from etc
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", Application["smtpserver"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Application["smtpserverport"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Application["sendusername"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Application["sendpassword"].ToString());
System.Net.Mail is recommended as the replacement, but it seems to have done away with these fields.
Here's my new email sending code.
MailMessage em = new MailMessage();
// ... fill in to, from etc
// Init SmtpClient and send
SmtpClient smtpClient =
new SmtpClient(
AppSetting["smtpserver"],
Convert.ToInt32(AppSetting["smtpserverport"])
);
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(
AppSetting["sendusername"],
AppSetting["sendpassword"]
);
smtpClient.Credentials = credentials;
smtpClient.Send(em);
I suspect that SmtpClient.Send is now doing all of this behind the scenes?
Yes, you don't have to add those fields manually.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential("gmailId", "Password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("gmailId", "Testing Mail");
mail.To.Add(new MailAddress("someMailId"));
mail.Body = "This is a test email. Please ignore or delete.";
mail.Subject = "Mail Testing";
smtpClient.Send(mail);
I am trying to use the code above to send mail. It was working fine for me, but when I tried to use it in another system it gave me an error ("Unable to connect to the remote server").
I think your gmail account is secured with mobile sms facility. If you access your gmail account from other macine than a sms will recieve from gmail for varification.
Please off this functionality and try again.
I'm trying to send and save the send email using C# code. But i can't get this done. I can either save the mail, or send it. But i can't get both done.
This is what i have:
public ActionResult Index()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("test#mail.com");
message.To.Add(new MailAddress("mymail#gmail.com"));
message.Subject = "Test Subject";
message.Body = "This is a test message";
message.IsBodyHtml = true;
// Setup SMTP settings
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential basicCredential = new NetworkCredential("mymail#gmail.com", "******");
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicCredential;
smtp.Send(message);
// save
smtp.EnableSsl = false;
smtp.PickupDirectoryLocation = #"C:\Temp";
smtp.Send(message);
return View();
}
So first i try to send the email. That works. Then i'm trying to save the email to my HDD. But it never gets saved. It does work when i don't send out the email and try to save it to my HDD right away. But i need to do both.
Anyone any idea how i can get this done? I just simply need to log the send messages.
Mail messages in the pickup directory are automatically sent by a local SMTP server (if present), such as IIS. (SmtpClient.PickupDirectoryLocation)
If you want to save to file system, you need to set the DeliveryMethod to SmtpDeliveryMethod.SpecifiedPickupDirectory:
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = #"C:\Temp";
client.Send(message);
See How to save MailMessage object to disk as *.eml or *.msg file
You have to change the property DeliveryMethod to SmtpDeliveryMethod.SpecifiedPickupDirectorynot to not send the email.
Just changing the PickupDirectoryLocation will not work, because the property is not used when DeliveryMethod is set to Network (which is the default value).
See MSDN.
I have the following code but I am getting an exception that a smtp host is not defined. If I am running this and testing on my local machine from visual studio, what do I need to do to be able to send email from my machine. Do I have to turn on some Windows service?
private void SendMailToAdminToApprove(string email_, string name_)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("address#domain.com", "Person's Name");
msg.To.Add(new MailAddress("a#gmail.com", "Adam"));
msg.Subject = "Message Subject";
msg.Body = "Mail body content";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
try
{
SmtpClient c = new SmtpClient();
c.Send(msg);
}
catch (Exception ex)
{
Console.Write("T");
}
}
You need to set the SMTP host to point to an actual SMTP server. One option is to run the SMTP service on your own machine, but you could also point to your ISP's server.
edit
As pcampbell and Skeolan mentioned, the actual value should go into app.config. I'm not sure if localhost would be an exception: it would depend on whether you want the option of not running a local server.
You'll need to specify the SMTP host here:
string smtpHost = "localhost";
//or go to your config file
smtpHost = ConfigurationManager.AppSettings["MySmtpHost"].ToString();
SmtpClient c = new SmtpClient(smtpHost);
You need to define the SMTP relay:
SmtpClient c = new SmtpClient("relay.yourdomain.com");
or if you're running the relay locally:
SmtpClient c = new SmtpClient("localhost");
You should change this section:
SmtpClient c = new SmtpClient();
// Either specify a SMTP server above, or set c.Host
c.Send(msg);
You need to specify which SMTP server is going to be used for sending this message. If you install a SMTP server locally, this could be localhost - but otherwise, you'll need to have an outgoing mail server set appropriately.
Here is the code I use for sending email with C#. I also commented out code for sending it to a file locally if you need it.
SmtpClient smtp = new SmtpClient(smtpServer, portNumber);
// Disable SSL when saving to directory.
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(mailFrom, password);
// Set mail to be delivered to a folder
//smtp.PickupDirectoryLocation = #"C:\mail\Send";
//smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;