Cannot send email with ASP.NET - c#

My Project need to send an e-mail but I cannot send it. I don't know why. Last month I can send but today I cannot.
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("***examplemail***", "myPassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("***examplemail***");
msg.To.Add(new MailAddress("***ToEmail***"));
msg.Bcc.Add(new MailAddress("***examplemail***"));
msg.Subject = "TEST";
msg.Body = "Hi, TEST Send E-mail";
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
** It didn't have any error but I didn't send too.

I wouldn't use the port number in here.
SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995
This way it sends. I tried and it worked. I am saying that it should look like
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you check SmtpClient's constructor while writing the code you will see that it has one overload.

Try this:
using System.Net;
using System.Net.Mail;
namespace consSendMail
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = new NetworkCredential("yourmail", "yourpassword")
};
var mailMessage = new MailMessage();
mailMessage.Subject = "Subject";
mailMessage.From = new MailAddress("yourmail", "yourname");
mailMessage.IsBodyHtml = true;
mailMessage.Body = "your message";
mailMessage.To.Add( new MailAddress("destinationemail") );
smtpClient.Send(mailMessage);
mailMessage.Dispose();
smtpClient.Dispose();
}
}
}

Related

Sending email from c#

I'm trying to make support form from C#. But when I send an email I get:
System.Net.Mail.SmtpException: 'Syntax error in parameters or
arguments. The server response was: 5.5.4 HELO/EHLO argument "/f"
invalid, closing connection.'
Here is my code;
private void button2_Click(object sender, EventArgs e)
{
using (MailMessage mailMessage = new MailMessage())
{
string email = YourEmail_box.Text;
mailMessage.From = new MailAddress(YourEmail_box.Text);
mailMessage.To.Add(Globals.supportemail);
mailMessage.Subject = "New Email from " + email.ToString() + " [Howling-Software]";
mailMessage.Body = this.subjectbox.Text;
mailMessage.IsBodyHtml = true;
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(Globals.credical_user, Globals.credical_pass);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
}
}
This is a working solution, and a bit cleaner solution :)
var message = new MailMessage(FromEmailAdress, ToEmailAdress);
message.Subject = subject;
message.Body = message;
using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
{
mailer.Credentials = new NetworkCredential(FromEmailAdress, password);
mailer.EnableSsl = true;
mailer.Send(message);
}

Send Email With C# (Cpanel Hosting)

I have bought a cPanel host and the SMTP server information is:
This is my code:
string smtpAddress = "mandane.hostcream.com";
int portNumber = 465;
bool enableSSL = true;
string emailFrom = "mahabadi#exirsec.ir";
string password = Authenitication.PassWord;
string emailTo = To.Text;
string subject = Subject.Text;
string body = Body.Text;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
When I run my code and click on the send button after 1 or 2 minutes this appears:
Additional information: Failure sending mail.
What am I doing wrong?
I think you missed something, try this:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential smtpCredentials = new NetworkCredential("email from","password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("email from");
MailAddress toAddress = new MailAddress("email to");
smtpClient.Host = "smpt host address";
smtpClient.Port = your_port;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = smtpCredentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 20000;
message.From = fromAddress;
message.To.Add(toAddress);
message.IsBodyHtml = false;
message.Subject = "example";
message.Body = "example";
smtpClient.Send(message);
It seems that you cannot reach Yahoo address on port 465, please check if this address reachable first because it appears to be a network issue.

Using MailMessage to send emails in C#

I am experiencing some problems sending emails with MailMessage. I have two email accounts, (account1#gmail.com, and account2#gmail.com) and I would like account2 to send an email to account one at a button click event.
This is what I have but it's not working. I'm getting and exception saying it's forbidden.
try
{
//do submit
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.Send(emailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I have a feeling it's a problem with the Smtp but I have no clue.
Try this:
using (MailMessage emailMessage = new MailMessage())
{
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
using (SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587))
{
MailClient.EnableSsl = true;
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.Send(emailMessage);
}
}
I added the port to the smtp client and enabled SSL.
If port 587 doesn't work, try port 465.
Try:
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtUsername.Text);
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
You have not define port number. it should be 587. and use enableSsl=true
as below:
SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);
Try adding a Port number 587 and enabling SSL on as Gmail uses “strict” SSL security. This means that we’ll always enforce that your other provider’s remote server has a valid SSL certificate.:
try
{
//do submit
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.EnableSsl = true;
MailClient.Send(emailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
MailMessage objMail = new MailMessage();
SmtpClient objSMTP = new SmtpClient("from.google.uk");
MailAddress objTo = new MailAddress("e-mail", "name");
string sql = "Select Naam, Mail from tblMail";
OleDbCommand cmdMail = new OleDbCommand(sql, cnnConnectie);
OleDbDataReader dtrMail = default(OleDbDataReader);
cnnConnectie.Open();
dtrMail = cmdMail.ExecuteReader();
while (dtrMail.Read())
{
objMail.To.Add(dtrMail["Mail"].ToString());
objMail.From = objTo;
objMail.Body = "test";
objMail.Subject = dtrMail["name"].ToString();
objSMTP.Send(objMail);
}
cnnConnectie.Close();

Sending an email programmatically through an SMTP server

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.
Server side, the relay IP addresses look correct. Scratching my head about what I'm missing.
MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);
mail.From = new MailAddress("no-reply#company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
("AdminName", "************");
smtp.EnableSsl = false;
if (fileuploadResume.HasFile)
{
mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
fileuploadResume.FileName));
}
smtp.Send(mail);
Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send.
For reference, here is my standard mail function:
public void sendMail(MailMessage msg)
{
string username = "username"; //email address or domain user for exchange authentication
string password = "password"; //password
SmtpClient mClient = new SmtpClient();
mClient.Host = "mailex.company.us";
mClient.Credentials = new NetworkCredential(username, password);
mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mClient.Timeout = 100000;
mClient.Send(msg);
}
Typically called something like this:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);
if (File.Exists(fullExportPath))
{
Attachment mailAttachment = new Attachment(fullExportPath); //attach
msg.Attachments.Add(mailAttachment);
msg.Subject = "Subj";
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.ASCII;
msg.Body = "Body";
sendMail(msg);
}
else
{
//handle missing attachments
}
var client = new SmtpClient("smtp.gmail.com", 587);
Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
EnableSsl = true
client.Send("sender mail adress","receiver mail adress", "subject", "body");
MessageBox.Show("mail sent");
This is Adil's answer formatted for C#:
public static class Email
{
private static string _senderEmailAddress = "sendermailadress";
private static string _senderPassword = "senderpassword";
public static void SendEmail(string receiverEmailAddress, string subject, string body)
{
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
EnableSsl = true
};
client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
}
catch (Exception e)
{
Console.WriteLine("Exception sending email." + Environment.NewLine + e);
}
}
}
You didn't specify the port.
smtp.Port = 1111; // whatever port your SMTP server uses
The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25.

An existing connection was forcibly closed by the remote host in SMTP client

I am about to give up debugging SMTP servers to send email... My code is the following
SmtpClient mailClient = new SmtpClient("plus.smtp.mail.yahoo.com", 465);
mailClient.EnableSsl = true;
MailMessage message = new MailMessage();
message.To.Add("aditya15417#hotmail.com");
message.Subject = "permias-tucson-contact-us";
mailClient.Credentials = new NetworkCredential("myemail#yahoo.com", "mypassword");
MailAddress fromAddress = new MailAddress(Email.Text, Name.Text);
message.From = fromAddress;
mailClient.Send(message);
You need to pass login credentials:
mailClient.Credentials = new NetworkCredential(Email.Text, password)
Here's a full working example:
public class Program
{
static void Main(string[] args)
{
using (var client = new SmtpClient("smtp.mail.yahoo.com", 587))
{
client.Credentials = new NetworkCredential("youraccount#yahoo.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#yahoo.com");
mail.To.Add("destaccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
}
}
}
Make sure you replace your account and password.

Categories