The problem: I get "???????" instead of display name of sender (display name is the utf8 text, also tried using System.Web.Mail.MailMessage but got the same result):
var fromAddress = new MailAddress("from#mail.com", "Some UTF8 text");
var toAddress = new MailAddress(toAddress);
var client = new SmtpClient
{
Host = "smpt.server",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, "password"),
Timeout = 5000
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = title,
Body = messageStr,
IsBodyHtml = true
};
Have you tried changing the constructor?
var fromAddress = new MailAddress("from#mail.com", "Some UTF8 text", System.Text.Encoding.UTF8);
MailAddress Constructor (String, String, Encoding)
Related
i'm actually trying to create registration page with verfication mail using MVC in visual studio, but here to send a message im getting
error : 'RandLform.Controllers.MailMessage' to 'System.Net.Mail.MailMessage' RandLform
public void SendVerficationLinkEmail(string emailID, string activationCode)
{
var VerifyUrl = "/User/VerifyAccount/" + activationCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, VerifyUrl);
var fromEmail = new MailAddress("lokeshkingdom4u#gmail.com", "Lokesh Pladugula");
var toEmail = new MailAddress(emailID);
var fromEmailPassword = "paisa007";
string subject = "Account created Succesfully!";
string body = "<br/>To verify your account, click on below link.<br/><br/> "+" "+link+"";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NewNetworkCredential(fromEmail.Address, fromEmailPassword)
};
using (var message = new MailMessage(fromEmail, toEmail)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
smtp.Send(message);
}
Lokesh Paladugula,
I recommend you to change password of your email account.
I'm not sure if this is actual error, please send proper error.
This question already has answers here:
MailMessage c# - How to make it HTML and add images etc?
(5 answers)
Closed 8 years ago.
Hi I am trying to send an email using Bold and Underline in my Message in C#
<b></b> <u></u>
How would I implement this into my code??
var fromAddress = new MailAddress("test#gmail.com", "test");
var toAddress = new MailAddress("test#gmail.com", "test");
const string fromPassword = "REMOVED";
const string subject = "Engineering Completed NewParts Project";
const string body = "Engineering has completed their data entry on the <b><u>NewParts</b></u> project on PDMTool. / ";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Thank you.
Fix your new MailMessage declaration like so:
new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true // this tells the message to be sent in HTML
}
Set the IsBodyHtml property to true:
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
}
I'm trying to send an email through SmtpClient using this code
var client = new SmtpClient("smtp.gmail.com", 465)
{
Credentials = new NetworkCredential("***#gmail.com", "password"),
EnableSsl = true,
};
client.Send("***#gmail.com", "***#gmail.com", "test", "testbody");
What I'm getting is smtpException "Message could not be sent".
System.Net.Mail.SmtpException: Message could not be sent. ---> System.Exception:Connectionclosed at System.Net.Mail.SmtpClient.Read () [0x000f9] in /private/tmp/source/bockbuild-mono-
I'm using Mono on Mac (OSX 10.9.2)
My credentials and host/port are correct.
Maybe I need to enable it through my gmail account somehow?
Thanks!
Use:
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Application requires that a use SendAsync rather than just Send. So i Made a Class CEmailServer and set up everything. So far Send works fine but when changing it to work with SendAsync it does not. I Created a Method to be called when a mail is sent and the userToken is in place but it keeps failing. i Can't find my error. Here is my code :
static bool mailSent = false;
//Method for Sending with attachment.
public void SendEmail(string Address, string Recipient, string Subject, string Body, string Dir)
{
var fromAddress = new MailAddress("someadress.kimberley#gmail.com", "My Company");
var toAddress = new MailAddress(Address, Recipient);
const string fromPassword = "password";
string subject = Subject;
string body = Body;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = "Test";
message.Attachments.Add(new Attachment(Dir));
smtp.SendAsync(message,userState);
message.Dispose();
}
}
//Method for Sending regular message without attachment.
public void SendEmail(string Address, string Recipient, string Subject, string Body)
{
var fromAddress = new MailAddress("someadress.kimberley#gmail.com", "My Company");
var toAddress = new MailAddress(Address, Recipient);
const string fromPassword = "password";
string subject = Subject;
string body = Body;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = "Message Sent";
smtp.SendAsync(message, userState);
message.Dispose();
}
}
//Method to be called when sending is complete
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
MessageBox.Show("Sending Canc");
}
if (e.Error != null)
{
MessageBox.Show("Error Sending Mail");
}
else
{
MessageBox.Show("Message sent.");
}
mailSent = true;
}
Thank you very much!
You are disposing the message before the send can complete. You should be disposing them in the SendCompleted callback event. Here's an example of the best way to dispose the client and the message.
Your code should look something like this:
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
};
smtp.SendCompleted += (s, e) => {
SendCompletedCallback(s, e);
smtp.Dispose();
message.Dispose();
};
string userState = "Test";
message.Attachments.Add(new Attachment(Dir));
smtp.SendAsync(message, userState);
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Send email with attachment from WinForms app?
Here is my script:
var fromAddress = new MailAddress("myemail#gmail.com");
var toAddress = new MailAddress("myemail#gmail.com");
const string fromPassword = "mypassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.Send(message);
}
It works well, yet I am yet to come up with a way to add an attachment. Yes, I know this site has examples, but I cannot find one that will send an attachment
Use the Attachments property.