none
c# Email from windows form. RRS feed

  • Question

  • Hello,

    I've been searching a lot about c# sending email, i would like to do like a page when the user writes in the label some text and the gmail would come go me with entered stuff, i't would be like Contact us form. Also i don't have database.

    Tuesday, May 8, 2018 1:15 PM

All replies

  • Well the first step is knowing how to send an email (via SMTP) via this page. You really can't create a page with fill-in fields this way though yet better (since you don't have a database) would be to have an email with text that ask for information and manually view the information.

    If you truly want to be full function e.g. provide fields (input tag) tag to read the information once returned to you then there are several steps to follow which include saving the message, reading inputs via HTMLAgility pack along with MsReader (installed via NuGet) (usage can be seen in my MSDN Code Sample) to a text file (since you don't have a database) or use Outlook automation to parse data from email messages.

    Side note 1, saving emails to .eml will usually need to be parsed as a text file even though there are libraries out there stating them can be are unreliable.

    Side note 2, working with gmail if this is a possibility you will need to setup permissions on Google to allow receivingand sending via SMTP.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, May 8, 2018 1:41 PM
    Moderator
  • If this is for a web site then it is better to ask in the asp.net forums; the "To" address should be in the web site's configuration so that the code-behind does not need to be changed when you need to change the "To" address. For desktop applications the "To" address can be in the application's settings. Either way, you might need to provide other information that would be better in the configuration/settings.

    How you send depends on the host you are using to send. So you must get information from them. Often you can use defaults.

    The following is from one of my websites, hopefully it will help.

    try
    {
        MailMessage mm = new MailMessage();
        mm.To.Add(new MailAddress(ConfigurationManager.AppSettings["MailMessageTo"]));
        mm.From = new MailAddress(TextBoxemail.Text);
        mm.Subject = "Contact Me";
        mm.Body = Message.Text;
        mm.IsBodyHtml = false;
        new SmtpClient().Send(mm);
        TextBoxSuccess.Visible = true;
    }
    catch (Exception ex)
    {
        Result.Text = "Error: " + ex.ToString();
        Result.Visible = true;
    }
    

    You did not ask about receiving email. If you need to do that, .Net does not have anything that makes it as easy as it is to send email. For receiving email using .Net we usually use the Tcpclient class and do the I/O ourselves.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 8, 2018 2:10 PM
  • Its not a web page, its windows form
    Tuesday, May 8, 2018 2:30 PM
  • Its not a web page, its windows form

    Why do you think we considered this a web page? The only thing that is web concentric would be a HTML version of the email body which can also be plain text. 

    If you want labels and TextBox controls to capture input that is an easy task. You can construct email in HTML or plain text by various methods. I would ask you to give it a try, run into issues show us what you tried. We are not here to write your code but instead point you in the right direction.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, May 8, 2018 2:49 PM
    Moderator
  • You still can use that sample I showed. Create a MailMessage object and then send it using SmtpClient().Send as in the example. Your catch block would be different. You must change how the "To" address is set. You will also want to have:

    using System.Net.Mail;



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 8, 2018 2:53 PM
  • Why do you think we considered this a web page?

    Because I asked if it is.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 8, 2018 3:05 PM
  • You gave me that script, how can i configurate it now? Where do i need to put like mine gmail or smthg? can you give a tutorial how to load it.
    Tuesday, May 8, 2018 3:05 PM
  • Hi GeyBen420,

    Thank you for posting here.

    Here is a simple example in Winform for your reference.

    private void BtnSend_Click(object sender, EventArgs e)
            {
                string smtpAddress = txBSMPT.Text;/*"smtp.mail.yahoo.com";*/
                int portNumber =Convert.ToInt32(txBPort.Text);
                bool enableSSL = true;
    
                string emailFrom = txBFrom.Text;
                string password = txBPassWord.Text;
                string emailTo = txBTo.Text;
                string subject = txBSubject.Text;
                string body = txBContent.Text;
    
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(emailFrom);
                    mail.To.Add(emailTo);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    //// Can set to false, if you are sending pure text.
    
                    //mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
                    //mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
    
                    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                    {
                        smtp.Credentials = new NetworkCredential(emailFrom, password);
                        smtp.EnableSsl = enableSSL;
                        smtp.Send(mail);
                    }
                }
            }

    Please note that the SMPT for GMail is smtp.gmail.com, port number is 587 and it support SSL.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 9, 2018 6:56 AM
    Moderator
  • Can you remake that user should type only the text, like:

    1Textbox = Username

    2Textbox = Password

    3Textbox = Ip

    4Textbox = More info

    And these textboxes should come to my email. Like what user typed in just send me a email.

    Wednesday, May 9, 2018 11:22 AM
  • Hi GeyBen420,

    Thank you for feedback.

    In my previous reply, all the information of email address which you send mail from, to, and the password, Port, the subject, body of mail could input in TextBox. I make a screenshot for your reference.

    All the txBFrom, txBPassWord, txBTo, txBSubject, txBContent are my textbox name.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, May 10, 2018 2:57 AM
    Moderator
  • Hello,

    I need a script which will be like a "Registration form" or we can call "Contact us".

    So there is few things that i don't know. I searched for email functions, and i don't have a database, and i don't want to do it, cause i don't have host. So i need that, user which opened this form in C# Windows, Could put his info into the labels, and the app will save all info and send it from my gmail, to my gmail, like a transfer copy & paste.


    • Edited by GeyBen420 Saturday, May 12, 2018 11:08 PM
    • Moved by CoolDadTxModerator Monday, May 14, 2018 2:45 PM Winforms related
    • Merged by Stanly Fan Monday, May 21, 2018 7:52 AM the same thread
    Saturday, May 12, 2018 11:06 PM
  • Hi GeyBen420,

    Thank you for posting here.

    Do you try the code I provided in your another thread?

    https://social.msdn.microsoft.com/Forums/en-US/0e5e2329-e15a-4ec4-a348-3f36678bf556/c-email-from-windows-form?forum=csharpgeneral

    In your screenshot, you use a ip address. I am confused about this. You want to send from gmail to gmail. What does the ip address do here?

    I do this in Winform. If you wanna more features, please provide more details about this. We will try our best to give you a solution.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 8:34 AM
    Moderator
  • I just need a text  which was writen in the label. The text from label has to be put on gmail, and sented to me
    Monday, May 14, 2018 3:35 PM
  • I need that the app won't need my email, just need the user email and message.
    Monday, May 14, 2018 3:42 PM
  • Your email address is needed be it visible or not. See my TechNet article on setting up for SMTP email.

    https://social.technet.microsoft.com/wiki/contents/articles/51529.working-with-smtp-email-with-c-part-1.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 14, 2018 4:33 PM
    Moderator