none
How to forward the email? RRS feed

  • Question

  • Now I have a winform which can send emails to mailboxes that are searched according to the conditions form SQL.And for the next goal, I hope that within the 7 days, if I haven't received the reply from the owner of the mailbox, I can forward another email on the basis of the first one.How can I do it?

    Below is the code that I send the mail:

            private void button2_Click(object sender, EventArgs e)
            {
                MailMessage msg = new MailMessage();
                System.Net.NetworkCredential aCred = new System.Net.NetworkCredential("affhofih092@163.com", "Zydhauhf");
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.163.com ";
                client.UseDefaultCredentials = false;
                client.Credentials = aCred;
                if (dataGridView1.RowCount > 0)
                {
                    for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                    {
                        msg.Subject = "TEST";
                        msg.Body = this.textBox3.Text + dataGridView1.Rows[i].Cells[0].Value.ToString();
                        msg.From = new MailAddress("affhofih092@163.com");
                        msg.To.Add(dataGridView1.Rows[i].Cells[4].Value.ToString());
                        client.Send(msg);
                    }
                }
            }

    Monday, June 25, 2018 6:25 AM

Answers

  • First of all: If these are real credentials, you should change it immediately.

    Then make consistent use of imports (using), referencing classes and names. And also of using this.

    Also you missed that MailMessage and SmtpSender implement IDisposable.

    And this is bad code. The single-responsibility principle can be applied to methods too. Then it is often called separation of concerns. A method should do one thing. Thus decouple your UI logic from your business logic.

    Create a EmailSender class. Instantiate it once. and use it. E.g.

    public class EmailSender 
    {
    	private NetworkCredential networkCredential;	
    	private string smtpHost;
    	
    	public EmailSender(string smtpHost, string userName, string password)	{	
    		
    		this.networkCredential = new NetworkCredential(userName, password);
    		this.smtpHost = smtpHost
    	}            
    	
    	public bool SendMessage(string sender string receiver, string subject, string body) 
    	{
    		bool result = false;
    		try
    		{
    			using (Smtpclient smtpClient = new SmtpClient()) 
    			{
    				client.Host = this.smtpHost;
    				client.UseDefaultCredentials = false;
    				client.Credentials = this.networkCredential;
    				using (MailMessage message = new MailMessage())
    				{
    					message.Subject = subject;
    					message.Body = body;
    					message.From = new MailAddress(sender);
    					message.To.Add(receiver);
    					smtpClient.Send(message);
    				}
    			}
    
    			result = true;
    		}
    		catch{
    			//todo: error handling
    		}
    		
    		return result;
    	}
    }
    
    public class FormX
    {
    	private void button2_Click(object sender, EventArgs e)
    	{
    		EmailSender emailSender = new EmailSender("smtp.contoso.com ", "someone@contoso.com", "password");
    		if (dataGridView1.RowCount > 0)
    		{
    			for (int i = 0; i < dataGridView1.RowCount - 1; i++)
    			{
    				emailSender.SendMessage(				
    					"someone@contoso.com",
    					dataGridView1.Rows[i].Cells[4].Value.ToString(),
    					"TEST",
    					this.textBox3.Text + this.dataGridView1.Rows[i].Cells[0].Value.ToString();				
    				);
    			}
    		}
    	}
    }
    And for your question: Without further information it's hard to tell. Cause SMTP only allows sending e-mail. For checking replies you need to implement POP or IMAP or Exchange. But this depends on whether you have read access on your e-mail host.

    • Marked as answer by ZXFzhu Monday, July 2, 2018 7:35 AM
    Monday, June 25, 2018 8:01 AM

All replies

  • First of all: If these are real credentials, you should change it immediately.

    Then make consistent use of imports (using), referencing classes and names. And also of using this.

    Also you missed that MailMessage and SmtpSender implement IDisposable.

    And this is bad code. The single-responsibility principle can be applied to methods too. Then it is often called separation of concerns. A method should do one thing. Thus decouple your UI logic from your business logic.

    Create a EmailSender class. Instantiate it once. and use it. E.g.

    public class EmailSender 
    {
    	private NetworkCredential networkCredential;	
    	private string smtpHost;
    	
    	public EmailSender(string smtpHost, string userName, string password)	{	
    		
    		this.networkCredential = new NetworkCredential(userName, password);
    		this.smtpHost = smtpHost
    	}            
    	
    	public bool SendMessage(string sender string receiver, string subject, string body) 
    	{
    		bool result = false;
    		try
    		{
    			using (Smtpclient smtpClient = new SmtpClient()) 
    			{
    				client.Host = this.smtpHost;
    				client.UseDefaultCredentials = false;
    				client.Credentials = this.networkCredential;
    				using (MailMessage message = new MailMessage())
    				{
    					message.Subject = subject;
    					message.Body = body;
    					message.From = new MailAddress(sender);
    					message.To.Add(receiver);
    					smtpClient.Send(message);
    				}
    			}
    
    			result = true;
    		}
    		catch{
    			//todo: error handling
    		}
    		
    		return result;
    	}
    }
    
    public class FormX
    {
    	private void button2_Click(object sender, EventArgs e)
    	{
    		EmailSender emailSender = new EmailSender("smtp.contoso.com ", "someone@contoso.com", "password");
    		if (dataGridView1.RowCount > 0)
    		{
    			for (int i = 0; i < dataGridView1.RowCount - 1; i++)
    			{
    				emailSender.SendMessage(				
    					"someone@contoso.com",
    					dataGridView1.Rows[i].Cells[4].Value.ToString(),
    					"TEST",
    					this.textBox3.Text + this.dataGridView1.Rows[i].Cells[0].Value.ToString();				
    				);
    			}
    		}
    	}
    }
    And for your question: Without further information it's hard to tell. Cause SMTP only allows sending e-mail. For checking replies you need to implement POP or IMAP or Exchange. But this depends on whether you have read access on your e-mail host.

    • Marked as answer by ZXFzhu Monday, July 2, 2018 7:35 AM
    Monday, June 25, 2018 8:01 AM
  • Thank you for your help!Do you mean that if I change my email server in the future, my code will go wrong?Because of the server requires a secure connection.

    For the question,may use Exchange.And If I use my own mailbox, will I have read access to it?

    Tuesday, June 26, 2018 1:38 AM
  • The big problem is that sending an email and reading emails are two very different activities, handled by different kinds of servers.  What you're talking about here is a relatively complicated operation involving a lot of different pieces.

    You will need to record the fact that you sent an email in a database somewhere.  Then, if you were using Exchange, you could write an Exchange rule to monitor all incoming emails.  If you find a response, you could enter that fact in your database.

    After that, you just need an application that runs once a day to scan your database, find the entries that do not have a response, and send the followup.

    It's going to be a lot of work.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, June 26, 2018 4:22 AM
  • Thank you for your reply!It gave me a basic understanding of the problem!!!!!Thanks!
    Tuesday, June 26, 2018 4:30 AM
  • And for your question: Without further information it's hard to tell. Cause SMTP only allows sending e-mail. For checking replies you need to implement POP or IMAP or Exchange. But this depends on whether you have read access on your e-mail host.

    Sending e-mail uses the SMTP protocol. For reading e-mails you need different protocols, like POP3 or IMAP. These protocols are in most unix-stlye operations systems separate services (a.k.a. servers). Thus you cannot use SMTP to check, whether you got an reply.

    And as Tim pointed out, you need obviously keep track of your sent e-mails.

    Tuesday, June 26, 2018 7:52 AM
  • I got these information. Thank you!☺☺☺
    Wednesday, June 27, 2018 1:57 AM