Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. sending mails

sending mails

Scheduled Pinned Locked Moved C#
csharphtmlcomhelp
16 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MemberDotNetting
    wrote on last edited by
    #1

    I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

    D B R S R 6 Replies Last reply
    0
    • M MemberDotNetting

      I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You have to make sure the port your using matches the specification of those other hosts. Google uses 587 for secured communication to the SMTP server. Hotmail and Yahoo will probably not use the same port numbers. Check with them.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      M 1 Reply Last reply
      0
      • M MemberDotNetting

        I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        Most SMTP servers require you to be an authorised user (i.e. if I use Clara.net as my ISP, I can use the smtp.clara.net relay, but if you don't, you can't). Gmail allows you to use your Gmail account details to log into SMTP; I'm not sure if Hotmail and Yahoo do something similar but if so, at the minimum, you will have to change the credentials you're trying to use.

        M 1 Reply Last reply
        0
        • D Dave Kreskowiak

          You have to make sure the port your using matches the specification of those other hosts. Google uses 587 for secured communication to the SMTP server. Hotmail and Yahoo will probably not use the same port numbers. Check with them.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          M Offline
          M Offline
          Matt Meyer
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

          Hotmail and Yahoo will probably not use the same port numbers

          Port 587 is a standard for SMTP servers (similar to port 25), usually referred to as the 'submission' port. It's set aside for authenticated users to submit messages to the e-mail service. It typically needs encryption, but I don't believe that's a requirement of the standard. https://www.ietf.org/rfc/rfc2476.txt[^]

          D 1 Reply Last reply
          0
          • M MemberDotNetting

            I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            If you have a search on Google you may find some free to use public smtp hosts. I did use a one a few years abck for a project - don't recall the name but worth a try.

            "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

            1 Reply Last reply
            0
            • M MemberDotNetting

              I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

              S Offline
              S Offline
              satalaj
              wrote on last edited by
              #6

              Hi, You can refer email sending code snippet in below link Send email using htomail Smtp used for HOTMail is SMTP: smtp.live.com port: 25

              M 1 Reply Last reply
              0
              • M Matt Meyer

                Dave Kreskowiak wrote:

                Hotmail and Yahoo will probably not use the same port numbers

                Port 587 is a standard for SMTP servers (similar to port 25), usually referred to as the 'submission' port. It's set aside for authenticated users to submit messages to the e-mail service. It typically needs encryption, but I don't believe that's a requirement of the standard. https://www.ietf.org/rfc/rfc2476.txt[^]

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                It may be standard, but that doesn't mean people have to use it. Yahoo uses different ports, depending on which service level your have. I think, last I checked anyway, one is using port 995 and the other 465???

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                M 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  It may be standard, but that doesn't mean people have to use it. Yahoo uses different ports, depending on which service level your have. I think, last I checked anyway, one is using port 995 and the other 465???

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  M Offline
                  M Offline
                  Matt Meyer
                  wrote on last edited by
                  #8

                  True, standards can be ignored but it's worth pointing out that the ports listed weren't just arbitrarily chosen by the service provider. 995 is used for encrypting the POP3 protocol (POP3S, another standard :-D ). Port 465 is an old remnant from Exchange's method of encryption. It's become common due to Outlook/Microsoft's support of Exchange over the Submission port standard in their mail clients. These days, both protocols are supported in Outlook. One other note about 465 is that it will break using System.Net.Mail.SmtpClient due to how the encryption was implemented. See the remarks section at http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx[^] for the details.

                  D 1 Reply Last reply
                  0
                  • M Matt Meyer

                    True, standards can be ignored but it's worth pointing out that the ports listed weren't just arbitrarily chosen by the service provider. 995 is used for encrypting the POP3 protocol (POP3S, another standard :-D ). Port 465 is an old remnant from Exchange's method of encryption. It's become common due to Outlook/Microsoft's support of Exchange over the Submission port standard in their mail clients. These days, both protocols are supported in Outlook. One other note about 465 is that it will break using System.Net.Mail.SmtpClient due to how the encryption was implemented. See the remarks section at http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx[^] for the details.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Yeah, but the entire point was he needs to check with each provider to make sure the port settings are correct and hes using the correct connection options.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    1 Reply Last reply
                    0
                    • S satalaj

                      Hi, You can refer email sending code snippet in below link Send email using htomail Smtp used for HOTMail is SMTP: smtp.live.com port: 25

                      M Offline
                      M Offline
                      MemberDotNetting
                      wrote on last edited by
                      #10

                      I will try thank you, thank you

                      1 Reply Last reply
                      0
                      • B BobJanova

                        Most SMTP servers require you to be an authorised user (i.e. if I use Clara.net as my ISP, I can use the smtp.clara.net relay, but if you don't, you can't). Gmail allows you to use your Gmail account details to log into SMTP; I'm not sure if Hotmail and Yahoo do something similar but if so, at the minimum, you will have to change the credentials you're trying to use.

                        M Offline
                        M Offline
                        MemberDotNetting
                        wrote on last edited by
                        #11

                        yes I have already tested it does not work with yahoo and hotmail: Exception: authetification required

                        D 1 Reply Last reply
                        0
                        • M MemberDotNetting

                          yes I have already tested it does not work with yahoo and hotmail: Exception: authetification required

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          So are you using the correct username and password with Yahoo and Hotmail?? I'm gussing your GMail account and password are not going to work with those providers.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          M 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            So are you using the correct username and password with Yahoo and Hotmail?? I'm gussing your GMail account and password are not going to work with those providers.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            M Offline
                            M Offline
                            MemberDotNetting
                            wrote on last edited by
                            #13

                            I'm sorry I misunderstood, and I will explain when I send a mail and the sender is a user with a gmail account it works, I sent mail to Yahho and hotmail without any problems but if I Chenge sender then there is the problem

                            D 1 Reply Last reply
                            0
                            • M MemberDotNetting

                              I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

                              R Offline
                              R Offline
                              RobCroll
                              wrote on last edited by
                              #14

                              Change this line of code:

                              public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa");

                              Note that the credentials are pointing to gmail. You get that when you stare at a passage of code long enough.

                              "You get that on the big jobs."

                              1 Reply Last reply
                              0
                              • M MemberDotNetting

                                I'm sorry I misunderstood, and I will explain when I send a mail and the sender is a user with a gmail account it works, I sent mail to Yahho and hotmail without any problems but if I Chenge sender then there is the problem

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #15

                                Yeah, and I'll say it again. The error message says that the username and password you used to login to the SMTP server were not good credentials. Either the username was spelled wrong or that account doesn't exist on that server or the password you specified for that username wasn't correct.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                1 Reply Last reply
                                0
                                • M MemberDotNetting

                                  I managed to send emails but if I change client.Host = "smtp.gmail.com" to client.Host = "smtp.hotmail.com" or client.Host = "smtp.yahoo.com"; it does that works only with gmail namespace mailing { public partial class SendMail : Form { public SmtpClient client = new SmtpClient(); public MailMessage msg = new MailMessage(); public NetworkCredential smtpCreds = new NetworkCredential("aa@gmail.com", "aaa"); public SendMail() { InitializeComponent(); } public void SendMails(string sendFrom, string sendTo, string subject, string body) { try { client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = smtpCreds; client.EnableSsl = true; MailAddress to = new MailAddress(sendTo); MailAddress from = new MailAddress(sendFrom); msg.Subject = txtSubject.Text; msg.Body ="<html><body>"+txtMail.Text+"</body></html>"; msg.IsBodyHtml = true; msg.From = new System.Net.Mail.MailAddress(txtFrom.Text); msg.To.Add(txtTo.Text); // Attachment piece_joint = new Attachment(@"txtpiece.Text", MediaTypeNames.Application.Pdf); AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body,null,MediaTypeNames.Text.Html); LinkedResource img = new LinkedResource(@txtpiece.Text, MediaTypeNames.Image.Jpeg); img.ContentId = "idimg"; alternate.LinkedResources.Add(img); msg.AlternateViews.Add(alternate); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; client.SendCompleted += new SendCompletedEventHandler(smtp_sendcompleted); client.SendAsync(msg,null); } catch (Exception ex) { MessageBox.Show("Failed to send this mail" +ex.Message); } } static void smtp_sendcompleted(object sender,AsyncCompletedEventArgs e) { if (e.Error != n

                                  B Offline
                                  B Offline
                                  Bernhard Hiller
                                  wrote on last edited by
                                  #16

                                  And of course John.Doe@hotmail.com is not allowed to send an email with the sender of the email set to John.Doe@gmail.com. That's not a requirement of the smtp protocol, but a requirement of any sensible provider, oh spammer!

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups