Link to home
Start Free TrialLog in
Avatar of dave_kny
dave_kny

asked on

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?
Avatar of cyberdevil67
cyberdevil67

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!
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..

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
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.
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
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.
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
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.
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.
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?
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.
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.

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.
Yes its not 25 and its over the SSL its // - smtp.gmail.com use port 465 or 587
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



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
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
ASKER CERTIFIED SOLUTION
Avatar of axawire
axawire

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol, if i had actually read the other responses before i posted i would know harwantgrewal already solved the prob way back when.
Avatar of dave_kny

ASKER

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..
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.
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?