I am trying to sent email from my web application (an e-commerce site). i enabled "Less secure app access" from my mail account. And enabled IMAP also from mail setting. but still sending email is blocked or failed. What can be the possible reason for this? or i missing something in the code section?
here is code:
var client = new SmtpClient("smtp.gmail.com", 587)
{
EnableSsl = true,
Credentials = new NetworkCredential(userName, password)
};
var sender = new MailAddress(senderEmail, senderName);
var receiver = new MailAddress(receiverEmail);
var message = new MailMessage(#sender, receiver);
IIRC then I followed this guidance to enable gmail smtp for my application.
Important is to have set two factor authentication for the account.
Create app password and use it in your application
then you use it as
MailMessage mm = new MailMessage();
mm.From = new MailAddress(<your-gmail>);
mm.To.Add(address);
mm.Subject = subject;
mm.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(<your-gmail>, <app-password>);
smtp.Send(mm);
I have some code that can only work with my SMTP host domains but not others. It seems to send, but it doesn't actually send anything and it also doesn't throw any exceptions.
SmtpClient mailClient = new SmtpClient();
mailClient.UseDefaultCredentials = false;
mailClient.Port = 587;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Host = "mail.smtpserver.com";
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("mail#smtpserver.com", "password");
mailClient.Credentials = cred;
MailMessage message = new MailMessage();
message.From = new MailAddress("mail#smtpserver.com");
message.To.Add("mail#remotedomain.com");
message.Subject = "Subject";
message.Body = "Body";
mailClient.Timeout = 200000;
mailClient.Send(message);
I think the problem is not checking NetworkCredential...
I would suggest you to try to send a testing mail with the "other" smtp server using telnet to make sure it works.
your smtp port is not the default one so maybe it is blocked by your firewall.
Generally, if you're using port 587 then that is SSL secured SMTP - so you need to change your code so that uses it when establishing the connection:
mailClient.EnableSsl = true;
Hi its been almost a day that I've been figuring things out with regards to sending email from godaddy email account to a gmail account. I have had my research online and almost tried everything but no luck .. here's what I made so far.
protected void generateEmail(){
MailMessage mail = new MailMessage ();
mail.From = new System.Net.Mail.MailAddress ("contact#company.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient ();
smtp.Port = 465; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential ("contact#company.com", "password123!"); // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
smtp.Host = "relay-hosting.secureserver.net";
//recipient address
mail.To.Add (new MailAddress ("test#gmail.com"));
mail.To.Add (new MailAddress ("testagain#gmail.com"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "This is a Test Message";
mail.Body = st;
smtp.Send (mail);
}
Can anyone help me out ? would appreciate any hands..
protected void generateEmail()
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("whereEmailWillBeSent#gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("mail#company.com");
msg.From = address;
msg.Subject ="Hi this is mail from company";
msg.Body = "Your Message";
SmtpClient client = new SmtpClient();
//for Godaddy
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
client.EnableSsl = false;
client.UseDefaultCredentials = false;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
}
}
I have gone through all the answers ... this is my situation
i need C# code to send email using ibm lotus account ( have username and password)
the server from which our app sends out emails is authorized
no firewall stopping
IBM lotus client is not installed on the server. so cannot use interop.domino.dll
the SMTP service is exposed. i have ip address and port. cant telnet to it and test it becasue server does not have telnet and they will not allow us enabling it
When i run the code below i get connection actively refused exception.
Is there any working code sample .. or am i missing something here .. any trouble shooting tips will be appreciated.
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from.Text);
message.To.Add(new MailAddress(to.Text));
//message.To.Add(new MailAddress("recipient2#foo.bar.com"));
//message.To.Add(new MailAddress("recipient3#foo.bar.com"));
//message.CC.Add(new MailAddress("carboncopy#foo.bar.com"));
message.Subject = "Test email from cogniti";
message.Body = "Test email from Cogniti";
SmtpClient client = new SmtpClient();
client.Port = Convert.ToInt32(port.Text);
client.Host = smtp.Text;
client.Credentials = new System.Net.NetworkCredential(username.Text, passwordBox1.Password);
//client.UseDefaultCredentials = true;
if (ssl.Text.Equals("1"))
client.EnableSsl = true;
else
if (ssl.Text.Equals("2"))
client.EnableSsl = false;
else
client.EnableSsl = false;
client.UseDefaultCredentials = false;
client.Send(message);
MessageBox.Show("Message Sent to: " + to.Text);
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
MessageBox.Show(e3.InnerException.ToString());
MessageBox.Show(e3.Source);
MessageBox.Show(e3.StackTrace);
}
Have you tried to move
client.UseDefaultCredentials = false;
before
client.Credentials = new System.Net.NetworkCredential(...
Apparently, if UseDefaultCredentials property isn't set before you actually set client credentials, the AUTH command wont be sent.
Possible Exact Duplicate: Sending Email in C#.NET Through Gmail
Hi,
I'm trying to send an email using gmail:
I tried various examples that I found on this site and other sites but I always get the same error:
Unable to connect to the remote server -- > System.net.Sockets.SocketException: No connection could be made because the target actively refused it 209.85.147.109:587
public static void Attempt1()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("MyEmailAddress#gmail.com", "MyPassWord"),
EnableSsl = true
};
client.Send("MyEmailAddress#gmail.com", "some.email#some.com", "test", "testbody");
}
Any ideas?
UPDATE
More details.
Maybe I should say what other attempts I made that gave me the same error:
(Note when i didn't specify a port it tryed port 25)
public static void Attempt2()
{
var fromAddress = new MailAddress("MyEmailAddy#gmail.com", "From Name");
var toAddress = new MailAddress("MyEmailAddy#dfdf.com", "To Name");
const string fromPassword = "pass";
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); }
}
public static void Attempt3()
{
MailMessage mail = new MailMessage();
mail.To.Add("MyEmailAddy#dfdf.com");
mail.From = new MailAddress("MyEmailAddy#gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("MyEmailAddy#gmail.com", "pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}
I'm using the following code:
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");//username doesn't include #gmail.com
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try {
sc.Send(mm);
} catch (Exception ex) {
EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
}
With the following code, it will work successfully.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#mydomain.com", "Enquiry");
mail.To.Add("abcdef#yahoo.com");
mail.IsBodyHtml = true;
mail.Subject = "Registration";
mail.Body = "Some Text";
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("xyz#gmail.com", "<my gmail pwd>");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
But, there is a problem with using gmail. The email will be sent successfully, but the recipient inbox will have the gmail address in the 'from address' instead of the 'from address' mentioned in the code.
To solve this, please follow the steps mentioned at the following link.
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:
http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html
Here is my connection resource to connect to Gmail from Java
<!-- Java Mail -->
<Resource name="mail/MailSession" auth="Container" type="javax.mail.Session"
mail.debug="true"
mail.transport.protocol="smtp"
mail.smtp.host="smtp.gmail.com"
mail.smtp.user="youremail#gmail.com"
mail.smtp.password="yourpassword"
mail.smtp.port="465"
mail.smtp.starttls.enable="true"
mail.smtp.auth="true"
mail.smtp.socketFactory.port="465"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
mail.smtp.socketFactory.fallback="false"
mail.store.protocol="pop3"
mail.pop3.host="pop.gmail.com"
mail.pop3.port="995" />
Connect your Gmail account on Secure ports (465 for SMTP and 995 for POP3) and use any .NET SSL Factory available to connect securely to Gmail.
Are you sure that your GMail account is set up to allow POP/SMTP connections? It is a configurable option that you can turn on and off as you choose.
You can see my blog post here at http://codersatwork.wordpress.com/2010/02/14/sending-email-using-gmail-smtp-server-and-spring-mail/ which explains how to use spring mail for sending email via gmail smtp server.
I used java but you can see the configuration and use that in your c# code.
Try using port number 465 for SSL connection