I'm using MVC3 and need to send an email to a user. I don't want to use a gmail server. But, I do want to use server 10.1.70.100. I don't understand what I am doing wrong. Here is my code:
var fromAddress = new MailAddress("sum1#abc.com", "From Name"); var toAddress = new MailAddress(EmailID, "To Name");
const string fromPassword = "";//To be Filled
const string subject = "Verification Mail";
string body = "You have successfully registered yourself. Please Enter your Verification code " + code.ActivatedCode;
var smtp = new SmtpClient
{
Host = "10.1.70.100",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(),
Timeout = 100000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Can someone suggest a way through which I don't have to give my credentials??
I prefer doing this on the following way:
Web.config:
<system.net>
<mailSettings>
<smtp from="Description">
<network host="your.smtpserver" password="" userName="" />
</smtp>
</mailSettings>
</system.net>
Your code:
var smtpClient = new SmtpClient();
smtpClient.Send(mail);
For our OSS project we use this little helper. Hope it helps.
public void SendEmail(string address, string subject, string message)
{
string email = ConfigurationManager.AppSettings.Get("email");
string password = ConfigurationManager.AppSettings.Get("password");
string client = ConfigurationManager.AppSettings.Get("client");
string port = ConfigurationManager.AppSettings.Get("port");
NetworkCredential loginInfo = new NetworkCredential(email, password);
MailMessage msg = new MailMessage();
SmtpClient smtpClient = new SmtpClient(client, int.Parse(port));
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
Related
System.IO.StringWriter myStringWriter = new System.IO.StringWriter(new System.Text.StringBuilder());
HtmlTextWriter myTextWriter = new HtmlTextWriter(myStringWriter);
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
string Body= sb.ToString();
MailMessage myMessage = new MailMessage();
myMessage.To.Add("edf#gmail.com");
myMessage.Subject = "Registration Successfully Completed";
myMessage.Body = Body;
myMessage.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
//smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "abc#46");
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Send(myMessage);
}
I am getting this error:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required.
Learn more at
Actually in my application i have a feedback form to get the complaints through email. So how can i achieve to get an email from each users to some particular email ("abed#gmail.com").
Usually, i use user account credentials to send email to others.
Code to send email
public ActionResult SendEmail(string SentTo, string Text)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("senderemail");
msg.To.Add(SentTo);
msg.Subject = "Password";
msg.Body = Text;
msg.Priority = MailPriority.High;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
//client.EnableSsl = false;
client.Credentials = new NetworkCredential("emailaddress", "password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
bool result = false;
try
{
client.Send(msg);
}
catch (Exception)
{
result = false;
}
return View();
}
But how can i receive an email from contact form?
Looking for some help to get the solution for my issue. Any help appreciated. Thanks in advance !!!
I am trying to send email from my controller (MVC4) through exchange server i.e. (outlook.office365.com as I can see it from my outlook settings) using SMTP but could not succeed. I tried solution from many previous posts but cant get any clue. I sincerely appreciate any help from you guys..
Here is my mail configuration:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.office365.com" userName="no-reply#mydomain.com" password="defaultPassword"/>
</smtp>
</mailSettings>
</system.net>
Here is my class for email:
public class eMail
{
public eMail() { }
private string Sender{ get; set; }
private List<string> Recipients { get; set; }
private string Subject { get; set; }
private string Body { get; set; }
public bool SendEmail()
{
try
{
var smptClient = new SmtpClient { EnableSsl = true };
MailMessage newEmail = new MailMessage();
foreach (var reciepent in this.Recipients )
newEmail.To.Add(new MailAddress(recipient));
newEmail.From = new MailAddress(this.Sender);
newEmail.Subject = this.Subject;
newEmail.Body = this.Body;
newEmail.IsBodyHtml = false;
smptClient.Send(newEmail);
return true;
}
catch { return false; }
}
}
in above code if I use "EnableSsl = true", I get error "Server does not support secure connections.". Still if I disable ssl, I get following error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
use this piece of code:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone#somedomain.com", "SomeOne"));
msg.From = new MailAddress("you#yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 25; // give 587 if 25 is blocked
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = ex.ToString();
}
Finally I'd resolve the issue which was related to domain we have.
For the person who are facing similar problem, here is how I had fixed it. Follwoing were my email settings:
<network host="smtp.office365.com" userName="no-reply#mydomain.com" password="defaultPassword"/>
If you are on domain then your username must have domain mentioned in your username i.e. no-reply#domain.mydomain.com and bang :)
Cheers!!
Folks,
I wanted to send a few emails with different subject and body asynchronously. here is my code
Email.cs
public string To;
public string CC;
public string Subject;
public string Host;
public string Port;
public string Body;
public MailMessage mail;
public SmtpClient smtp;
public void send()
{
smtp = new SmtpClient();
mail = new MailMessage();
mail.To.Add(To);
if (this.CC !="" && this.CC !=null) mail.CC.Add(CC);
mail.CC.Add(CCIDBizzMail);
mail.Subject = this.Subject;
mail.From = new MailAddress(From);
mail.IsBodyHtml = true;
smtp.Host = this.SMTPAddress;
mail.Body = this.Body;
smtp.Credentials = new System.Net.NetworkCredential
(this.From, this.Password);
smtp.EnableSsl = false;
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
smtp.SendAsync(mail, null);
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
String token = (string)e.UserState;
if (e.Cancelled)
{
}
if (e.Error != null)
{
}
else
{
mail.Dispose();
smtp.Dispose();
}
}
here is my code to send an email:
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "first email";
objEmail.To: "ssss#mail.com"
objEmail.Send();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "second email";
objEmail.To: "tttt#mail.com"
objEmail.Send();
However, tttt#mail.com never received an email. my website always send to ssss#mail.com
can you help me to solve this issue?
Here you try to send an email with Asynchronous type with out making a new object Email.
I suggest to try two thinks.
Make new on every email send
{
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "first email";
objEmail.To: "ssss#mail.com"
objEmail.Send();
}
{
Email objEmail = new Email();
objEmail.Subject = "Thank You for Your Order!";
objEmail.Body = "second email";
objEmail.To: "tttt#mail.com"
objEmail.Send();
}
Or change the Email routine to
public string To;
public string CC;
public string Subject;
public string Host;
public string Port;
public string Body;
public void send()
{
using(var smtp = new SmtpClient())
{
using(mail = new MailMessage())
{
mail.To.Add(To);
if (this.CC !="" && this.CC !=null) mail.CC.Add(CC);
mail.CC.Add(CCIDBizzMail);
mail.Subject = this.Subject;
mail.From = new MailAddress(From);
mail.IsBodyHtml = true;
smtp.Host = this.SMTPAddress;
mail.Body = this.Body;
smtp.Credentials = new System.Net.NetworkCredential
(this.From, this.Password);
smtp.EnableSsl = false;
// maybe here you place extra code for the errors
// http://msdn.microsoft.com/en-us/library/swas0fwc.aspx
smtp.Send(mail);
}
}
}
If the email is send using localhost and if you like to send many emails, is better to send them right way and not asynchronous.
I want to send a view in email body in mvc.
please guide how to render view so that it can be sent as html email body.
thanks,
Consider using something like ActionMailer. You can download it using NuGet.
string path = ConfigurationManager.AppSettings["ProjectPath"] ;
string gmailpath = path + "/" + "Driver/VerificedAccount?code=" + root.Result.EmailVerificationCode;
var body= "<html><body><p></p><p><a href = "+gmailpath+" > Please click Verifed Account </a></p></body></html> ";
var st = EmailclassHtml(sendemail.Email, "Verification-Driver", body);
public string EmailclassHtml(string email, string subjectname, string messgae)
{
string ownemail = ConfigurationManager.AppSettings["SenderEmail"];
string ownname = ConfigurationManager.AppSettings["SenderName"];
string returnmessage = "success";
var senderEmail = new MailAddress(ownemail, ownname);
var receiverEmail = new MailAddress(email, "Receiver");
var password = ConfigurationManager.AppSettings["SenderPassword"];
var sub = subjectname;
var body = messgae;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new NetworkCredential(senderEmail.Address, password)
};
using (var mess = new MailMessage(senderEmail, receiverEmail)
{
Subject = sub,
Body = body,
IsBodyHtml = true
})
try
{
smtp.Send(mess);
return returnmessage;
}
catch (Exception)
{
returnmessage = "fail";
}
return returnmessage;
}