I'm just wondering if i have the correct code to check if a proxy supports SMTP or not. I'm not a spammer or anything it just randomly interested me. I Have this for testing:
//host = proxy IP address
public static void can_mail(string host)
{
MailMessage msg = new MailMessage();
msg.Body = "Email is working!";
msg.From = new MailAddress("me#whatever.com");
msg.IsBodyHtml = false;
msg.Subject = "Mail Test";
msg.To.Add(new MailAddress("myemailaddress#myserver.com"));
try
{
SmtpClient client = new SmtpClient();
client.Host = host;
client.Port = 25;
client.EnableSsl = false;
client.Send(msg);
Console.WriteLine("{0} connected, mail probably works?", host);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
Have I missed something here?
Related
I am trying to send mail from Web API using SMTP (GODaddy). It failed to send the mail and returning the exception as follows
Code :
public void SendMail()
{
try
{
MailMessage mail = new MailMessage("support#abc.com","toMail");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("support#abc.com", "****");
client.Port = 25;
client.Send(mail);
}
catch (SmtpFailedRecipientException Ex)
{
Ex.FailedRecipient.ToString();
}
}
Can anyone help me to fix this.
I'm using the following basic code:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.to.add("someone#hotmail.com");
msg.to.add("someone#gmail.com");
msg.to.add("someone#myDomain.com");
msg.From = new MailAddress("me#myDomain.com", "myDomain", System.Text.Encoding.UTF8);
msg.Subject = "subject";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "body";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Host = "192.168.0.24";
client.Credentials = new System.Net.NetworkCredential("me#myDomain.com", "password");
client.Port = 25;
try
{
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
sw.WriteLine(string.Format("ERROR MAIL: {0}. Inner exception: {1}", ex.Message, ex.InnerException.Message));
}
Problem is the mail is only sent to the address in my domain (someone#mydomain.com) and I get the following exception for the 2 other addresses:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such domain at this location
I suspect it's something to do with something blocking my smtp client but not sure how to approach this.
Any idea? thanks!
Ron is correct,just use the 587 port and it will work as u wish.
Check this code and see if it works:
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address#mfc.ae");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Try using port=587. Here's useful related link: http://mostlygeek.com/tech/smtp-on-port-587/comment-page-1/
i've tried to send email using this code..but an error occurred in smtp.Send(mail); messaging "Failure sending mail"
MailMessage mail = new MailMessage();
// set the addresses
mail.From = new MailAddress("from#gmail.com");
mail.To.Add(new MailAddress("to#yahoo.com"));
// set the content
mail.Subject = "test sample";
mail.Body = #"thank you";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials = new NetworkCredential("from#gmail.com", "password");
smtp.Send(mail);
In your code specify port number:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)
Also check out this thread Sending email through Gmail SMTP server with C#
You need to set smtp.EnableSsl = true for gmail.
Take a look at this class, it should work for you:
public class Email
{
NetworkCredential credentials;
MailAddress sender;
public Email(NetworkCredential credentials, MailAddress sender)
{
this.credentials = credentials;
this.sender = sender;
}
public bool EnableSsl
{
get { return _EnableSsl; }
set { _EnableSsl = value; }
}
bool _EnableSsl = true;
public string Host
{
get { return _Host; }
set { _Host = value; }
}
string _Host = "smtp.gmail.com";
public int Port
{
get { return _Port; }
set { _Port = value; }
}
int _Port = 587;
public void Send(MailAddress recipient, string subject, string body, Action<MailMessage> action, params FileInfo[] attachments)
{
SmtpClient smtpClient = new SmtpClient();
// setup up the host, increase the timeout to 5 minutes
smtpClient.Host = Host;
smtpClient.Port = Port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Timeout = (60 * 5 * 1000);
smtpClient.EnableSsl = EnableSsl;
using (var message = new MailMessage(sender, recipient)
{
Subject = subject,
Body = body
})
{
foreach (var file in attachments)
if (file.Exists)
message.Attachments.Add(new Attachment(file.FullName));
if(null != action)
action(message);
smtpClient.Send(message);
}
}
}
fill
mail.Host
and
mail.Port
Properties with proper values
You should be using the using statement when creating a new MailMessage, plus a few things you missed out like port number and enableSSL
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("from#gmail.com");
mail.To.Add(new MailAddress("to#yahoo.com"));
mail.Subject = "test sample";
mail.Body = #"thank you";
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential("from#gmail.com", "password");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
Here's a basic GMAIL smtp email implementation I wrote a while ago:
public static bool SendGmail(string subject, string content, string[] recipients, string from)
{
bool success = recipients != null && recipients.Length > 0;
if (success)
{
SmtpClient gmailClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("******", "*****") //you need to add some valid gmail account credentials to authenticate with gmails SMTP server.
};
using (MailMessage gMessage = new MailMessage(from, recipients[0], subject, content))
{
for (int i = 1; i < recipients.Length; i++)
gMessage.To.Add(recipients[i]);
try
{
gmailClient.Send(gMessage);
success = true;
}
catch (Exception) { success = false; }
}
}
return success;
}
It should work fine for you, but you'll need to add a valid gmail acocunt where I've marked in the code.
This is the function which i checked to send mail...and it's working properly.
`
private static bool testsendemail(MailMessage message)
{
try
{
MailMessage message1 = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
MailAddress fromAddress = new MailAddress("FromMail#Test.com");
message1.From = fromAddress;
message1.To.Add("ToMail#Test1.com");
message1.Subject = "This is Test mail";
message1.IsBodyHtml = true;
message1.Body ="You can write your body here"+message;
smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("SenderMail#yahoo.com", "YourPassword");
smtpClient.Send(message1);
}
catch
{
return false;
}
return true;
}`
Thank You.
Following is the C# code for Gmail Service
using System;
using System.Net;
using System.Net.Mail;
namespace EmailApp
{
internal class Program
{
public static void Main(string[] args)
{
String SendMailFrom = "Sender Email";
String SendMailTo = "Reciever Email";
String SendMailSubject = "Email Subject";
String SendMailBody = "Email Body";
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage email = new MailMessage();
// START
email.From = new MailAddress(SendMailFrom);
email.To.Add(SendMailTo);
email.CC.Add(SendMailFrom);
email.Subject = SendMailSubject;
email.Body = SendMailBody;
//END
SmtpServer.Timeout = 5000;
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new NetworkCredential(SendMailFrom, "Google App Password");
SmtpServer.Send(email);
Console.WriteLine("Email Successfully Sent");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
}
}
For reference: https://www.techaeblogs.live/2022/06/how-to-send-email-using-gmail.html
I can't send a email message using gmail settings. i already tried client.Host ="localhost" it's working but not in client.Host ="smtp.gmail.com".. Please help me guys.. I need use client.Host ="smtp.gmail.com".. thanks
here's my C# code:
string from = "aevalencia119#gmail.com"; //Replace this with your own correct Gmail Address
string to = "aevalencia191#gmail.com"; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to); mail.From = new
MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ; mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient(); //Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple");
client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
} HttpContext.Current.Response.Write(errorMessage
);
} // end try
here's the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Much thanks guys!
You need to get and send mail to GMail by using SSL secutiry certificate
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}
reference: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "aevalencia119#gmail.com";
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = new MailAddress( "aevalencia119#gmail.com");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("aevalencia119#gmail.com");
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
client.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("aevalencia119#gmail.com", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("myname#gmail.com", "Lenin");
smtpClient.Host = "localhost";
//smtpClient.Host = "";
//smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("myname#gmail.com");
message.Subject = "Feedback";
//message.CC.Add("admin1# gmail.com");
//message.CC.Add("admin2# gmail.com");
// message.Bcc.Add(new MailAddress("admin3# gmail.com"));
// message.Bcc.Add(new MailAddress("admin4# gmail.com"));
message.IsBodyHtml = false;
message.Body = txtComments.Text;
smtpClient.Send(message);
MessageBox.Show("Email successfully sent.");
}
catch (Exception ex)
{
MessageBox.Show("Send Email Failed." + ex.Message);
}
please help to sent email to different server .. gmail.com/yahoo.com/inbox.com ..etc using windows application.
thanks for the help.
SmtpClient smtpClient = new SmtpClient(host, port);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username#gmail.com", "password");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 465 or 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(MailMessage);
try this one.