Results 1 to 11 of 11

Thread: [RESOLVED] Want to send an email - can I just figure it out without anyone helping me...

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Resolved [RESOLVED] Want to send an email - can I just figure it out without anyone helping me...

    ... other than you guys here?

    I'm working on a new function in my WinForms C# application to send an email. I send emails in this app all the time but how it works is we have a table called xtblMessages and I just write a row into the table that has a subject, message body, recipient, etc. and some program that somebody else wrote processes the table. What I need to do this time is attach an Excel file, and the table doesn't support that.

    I've emailed the author of that code twice today but he hasn't replied, so I was wondering if this is something I can just figure out, or if I need him or our other IT person to tell me what goes here?

    Code:
                try
                {
                    //do submit
                    MailMessage emailMessage = new MailMessage();
                    emailMessage.From = new MailAddress("account2@gmail.com", "Account2");
                    emailMessage.To.Add(new MailAddress("account1@gmail.com", "Account1"));
                    emailMessage.Subject = "SUBJECT";
                    emailMessage.Body = "BODY";
                    emailMessage.Priority = MailPriority.Normal;
                    SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
                    MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
                    MailClient.Send(emailMessage);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    Even if it's just temporary to flesh everything out and I change it later, if I could get this to work I'd be making progress. I didn't know if I could use my own personal gmail account while testing, but I wasn't thrilled about putting my personal password here: MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password") even temporarily.

    Thanks for any tips.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Want to send an email - can I just figure it out without anyone helping me...

    I typically use papercut to send SMTP locally for testing. You can just set the client to localhost and you don't need to use credentials: https://github.com/ChangemakerStudios/Papercut-SMTP

    It looks pretty straightforward to add an attachment to the MailMessage object. https://docs.microsoft.com/en-us/dot...s?view=net-5.0

    And attachment seems to have plenty of constructors you can use if you want to do this all in memory, or read a file from the filesystem and pass the contents: https://docs.microsoft.com/en-us/dot...t?view=net-5.0

    Is there something specific you're getting stuck on?

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Want to send an email - can I just figure it out without anyone helping me...

    If you don't want credentials hard-coded (which I would be nervous about as well), you should just create a small form that asks for the username/password and populates the Credentials with what was entered.

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    I am getting stuck on what provide in this line: MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
    Would I use my own personal gmail id and pw?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    Quote Originally Posted by OptionBase1 View Post
    If you don't want credentials hard-coded (which I would be nervous about as well), you should just create a small form that asks for the username/password and populates the Credentials with what was entered.
    Sorry I did not see this; I didn't refresh the page. Yes this might be a good idea. I can also change my gmail password when I'm done testing. Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  6. #6
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Want to send an email - can I just figure it out without anyone helping me...

    Quote Originally Posted by MMock View Post
    I am getting stuck on what provide in this line: MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
    Would I use my own personal gmail id and pw?
    If you use papercut, you don't need to specify credentials at all, you can just set SmtpClient("localhost") and it will deliver the mail to papercut for you to view. No need to worry about credentials at all.

  7. #7

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    Thanks @kfc, I will give that a try.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    @kfcSmitty, I don't understand exactly what I am supposed to do with the github code. Build and install it? I had errors when I tried to open the solution file I downloaded.

    I went to the papercut website https://www.papercut.com/support/res...ure-email.html to see if that would help, and the first screenshot here is how to configure an SMTP server using gmail. How is this any better than just going to gmail directly?

    I apologize if I misunderstood the answer you are proposing.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Want to send an email - can I just figure it out without anyone helping me...

    Just download the release here: https://github.com/ChangemakerStudio...-SMTP/releases

    You can get it as a standalone in the zip or an installer in the .exe. Then all you need to do is run the Papercut app and it will look like an email client.. any email sent to localhost at that point will show up there.

  10. #10

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    Ok, thanks, but I went with using gmail directly in my code. I realized I could make a new gmail account specifically for this and not have to use my own personal one. It's all working great. Later when we decide how we want to do this permanently I can just replace the code block. I will post back at that time when the final, real solution is and will mark resolved then. Thanks to you both for the discussion.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Want to send an email - can I just figure it out without anyone helping me...

    I was given the info I need which is our permanent solution as long as the mail is sent from a PC in our building. For this particular function in the application, that is fine.
    The SMTP Server is an outlook server, obviously the port had to be changed from the gmail server port and there's no authentication required. So really I just had to replace a few lines of code from the gmail-specific code.
    Thanks again for your interest in and help with my problem
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width