send mail using smtp in c#

is there anyway to send mail using yahoo or hotmail or others mail server?
here my code,

System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
                  message.From="xxx@yahoo.com" ;
                  message.To="xx@yahoo.com" ;
                  message.Subject="ddddd";
                  message.BodyFormat = MailFormat.Text;
                  message.Body="yyyyy"
                  
                  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","smtp.mail.yahoo.com");
                  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",25);
                  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",2);
                  message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
                  message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","username");
                  message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","password");
                  System.Web.Mail.SmtpMail.SmtpServer="smtp.mail.yahoo.com";

but its can't work...
anything wrong with my code?
dave_knyAsked:
Who is Participating?
 
axawireCommented:
ok, i found this code that i (converted to vb.net because thats what i am using) successfully sent an email to my home accout from google using there smtp server code.  The original code was in c# and is soley thanks to http://blogs.wdevs.com/qc/archive/2005/01/14/1951.aspx

public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {
 // Mail initialization MailMessage mail = new MailMessage();
 mail.From = from;
 mail.To = to;
 mail.Cc = cc;
 mail.Bcc = bcc;
 mail.Subject = subject;
 mail.BodyFormat = MailFormat.Text;
 mail.Body = body;
 // Smtp configuration
 SmtpMail.SmtpServer = "smtp.gmail.com";
 // - smtp.gmail.com use smtp authentication
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
 // - smtp.gmail.com use port 465 or 587
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
 // - smtp.gmail.com use STARTTLS (some clients call this SSL)
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
 // Mail sending
 try {
   SmtpMail.Send(mail);
   return "";
 } catch (Exception ex) {
   return ex.Message;
}
0
 
cyberdevil67Commented:
Hi dave_kny,

 when sending mail you still need to use your ISP for that, this is usually blocked by ISP's because of spam. So you could receive via that server, but to send via that server is considered BAD and is usually blocked to stop people from sending mail through their servers.


Cheers!
0
 
harwantgrewalCommented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
harwantgrewalCommented:
And this too This will sure help yo

http://blogs.wdevs.com/qc/archive/2005/01/14/1951.aspx

Harry
0
 
cyberdevil67Commented:
Yes you can not use the smtp server of another mail server when you are not connected to that server directly.

System.Web.Mail.SmtpMail.SmtpServer="smtp.mail.yahoo.com";

Need to point to your mail server..

0
 
harwantgrewalCommented:
I guess this shouldt be true as I can configure my OUTLOOK to any smtp server on my ISP if I have smtp access to that provider. For example yahoo smtp service is paid service and if I am a paid member I can configure my OUTLOOK to yahoo. Gmail is free service and the second URL gives the example for gmail. So I guess its not true that ISP is blocked if one can Access that email from the same ISP through OUTLOOK

Harry
0
 
cyberdevil67Commented:
Actually no...

I have a work account and a normal ISP account, and if I need to send email to my work account I need to send it via my ISP account.

These as I said are blocked, because Mail Relaying is not allowed.
0
 
harwantgrewalCommented:
That means if you want to send email to any email address you have to sent it via your ISP? Like I have an ISP account and I am using gmail to send email without any issues from OUTLOOK. If you are talking about relay let me explain a little here. I am ISP I have smtp installed I made configuration that only IP of my range can relay the email if they are using my SMTP, but in case of gmail or yahoo you are using their SMTP and they relay message for authenticated users. The only case you can not send your email from OUTLOOK if that port/URL is blocked and again that is a Firewall configuration not Relay configuration. So one can check by configuring the OUTLOOK.
Thanks

Harry
0
 
cyberdevil67Commented:
Define Authenticated.

If you are ISP then you will know the answer to your question. As I said I am authenticated with my work account and my ISP account. And when I am at home and wish to send an email via my work account I HAVE TO SEND IT via my ISP account.

So if yahoo, hotmail, gmail allowed this could you imagine the spam that would be sent via these accounts, how do they authenticate you if you do not log into there servers.

As I have written my own mail server I know what I am talking about.
0
 
harwantgrewalCommented:
Thats very nice that you have written your own mail server. I havnt that much experience, may be what I am trying to communicate is not proper. Can we talk with example. I have a ISP account I am authenticated with ISP now I want to send email via my work account isnt it that I have to be authenticated with SMTP not with ISP? the username and password of my work SMTP is provided then where ISP play now?

Harry
0
 
cyberdevil67Commented:
NO, NO, NO

YOU CAN NOT CONNECT TO ANOTHER SMTP SERVER through an ISP that does not allow mail relay, and mail relay's are normally disallowed.

Now because you do not have a direct connection to you work account mail server, yahoo, hotmail account you can not relay mail and HAVE to use your ISP's smtp mail server.
0
 
cyberdevil67Commented:
We set clients up all the time and they can not understand why they have to use their ISP for sending mail, when we explain to them that if this was allowed the world would be full of spam they tend to understand.

And yes we are a sort of ISP for mail to our clients.
0
 
harwantgrewalCommented:
Okay that means you are saying if I send a email from my home it goes to SMTP ISP and replayed to gmail and then gmail authenticate the user and send email? if ISP has blocked reply to gmail then it wont go?
0
 
cyberdevil67Commented:
No,

You can send via your ISP smtp server, as you are already authenticated when you logged on to your ISP.

However if you want to connect to gmail, you need to know the server name, the port name, and the password for the account to do this. You can not just use a normal smtp account, if you want to do this you need to use your ISP.
0
 
harwantgrewalCommented:
oh BOY sorry to say that I guess you dont know that gmail gives that, thats why I was wondering why you are saying to use your ISP smtp. If you look into gmail and yahoo they provide all the information the URL port and username and password to authenticate. Thats how the code is working.

0
 
cyberdevil67Commented:
No I do know that...

If you want to use the standard port 25, I not sure the port gmail uses put I know its not 25.
0
 
harwantgrewalCommented:
Yes its not 25 and its over the SSL its // - smtp.gmail.com use port 465 or 587
0
 
sun4sundayCommented:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("luckyperson@online.microsoft.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);


check here for more details
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=651 


this shows a simpe application to send mails
http://www.codeproject.com/csharp/sendmailcsharp.asp

sun4sunday



0
 
thoffmanCommented:
While it is true that most SMTP servers prevent relaying, not all of them do. While is is true that many ISPs block port 25 for outgoing SMTP, again, not all ISPs block it.

The best thing to do is try an app like Outlook. If you can get Outlook sending email to the target server through your ISP, then nothing is blocked, and there is a problem in your code that is preventing it. On the other hand, if you can't get Outlook to send it, there's no point in trying to debug your code, since it will also fail.

--
Troy
0
 
axawireCommented:
hi, i dont know if this will help you but...

I think its 99% prob that your isp is blocking you from connecting to "smtp.mail.yahoo.com" on port 25

your code:
  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","smtp.mail.yahoo.com");
  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",25);

so...
yahoo offers another port if this is the case, port 587
  message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",587);

second yahoo states that if your "from" field is not the email address associated with the "username" and "password" it will not send the email.

so
  message.From="xxx@yahoo.com" ; //has to actually be a vaild email that belongs with the "username" and "password"

but you were probably just hiding the account information anyway...

good luck, post if it works out for you, im trying to do something along the same lines
0
 
axawireCommented:
lol, if i had actually read the other responses before i posted i would know harwantgrewal already solved the prob way back when.
0
 
dave_knyAuthor Commented:
Thanks for active reply from harwantgrewal,axawire and cyberdevil67.
i try all the solution u all provide and found that using the gmail smtp is works..
0
 
markwaugh365Commented:
i heard that due to gmail modifications, this may not be possible, can anyone verify that this still works, just wondering if its possible anymore.
0
 
dave_knyAuthor Commented:
markwaugh365,
i try this code and not working anymore
do you know wat modification they do? any other way to send mail using other smtp mail server?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.