3

I have been sending email from C# and they are working fine, but now I am also looking for the functionality to allow receiver to unsubscribe. I am unable to get how to proceed as I have no idea. I googled enough so need some hint.

Below is my code:

  MailMessage loginInfo = new MailMessage();
  loginInfo.To.Add(EmailTxt.Text.ToString());
  loginInfo.From = new MailAddress(sEmailId);
  loginInfo.Subject = "Subject";
  loginInfo.Body = "Your username is: " +;
  loginInfo.IsBodyHtml = true;

  SmtpClient smtp = new SmtpClient();
  smtp.Host = sHost;
  smtp.Port = 25;
  smtp.EnableSsl = false;
  smtp.Credentials = new System.Net.NetworkCredential("", "");
  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  smtp.Send(loginInfo); 
Incredible
  • 3,495
  • 8
  • 49
  • 77
  • 3
    Just send them a link to unsubscribe... am I missing something? – germi Nov 08 '13 at 08:26
  • 4
    How does this code help you to "unsubscribe" from whatever you want to be able to unsubscribe from? – Mithrandir Nov 08 '13 at 08:27
  • i hope OP shows the code for sending Email and asking the best approach to unsubscribe, please correct me if im wrong. – Sudhakar Tillapudi Nov 08 '13 at 08:28
  • 1
    You need to log users to a table with a boolean/bit unsubscribe column. the default would be 0 once they click on unsubscribe then you will update this column to 1 and send your email according to this column – Zaki Nov 08 '13 at 08:35
  • @ Sudhakar: Thanks yes you are correct. And if some one had read properly than I am showing what I know and also written what I want to know. :) – Incredible Nov 08 '13 at 09:36
  • @ Sam: Yes I get and just wanted to know how will I be catching their click in my web application? Or do I need to make another application to get when they click. – Incredible Nov 08 '13 at 09:37

3 Answers3

6

Unsubscribing is easy if you are already maintaining the users email id's in Database.

Please follow the below steps:

Step 1: Create an extra column in user table as unsubscribe in database. it will take true or false as values. set default to false so that every subscribed user gets email.

Note : Before sending Mails to users please check their unsubscribe column. if it is false send an Email. if it is true don't send an Email as they have unsubscribed.

Step2: Create an unsubscribing URL as below:

http://mywebsite.com/unsubscribeme/emailID=xyz@gmail.com

Step3: Send this URL to user as unsubscribe URL so that whenever he feels to unsubscribe he can do that simply by clicking on that URL.

Step4: once if user clicks on given URL read the QueryString value of emailID emailID=xyz@gmail.com

Step5: Update the user table info by setting the unsubscribe column value to true.

Example :

 //get user EmailID by QueryString as below:
 String EmailID=Reques.QueryString["emailID"].ToString();

//Update the usertable as below:
String Command ="update usertable set unsubscribe='true' where emailid='"+EmailID+"'";
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • I get, but there is one doubt, how will I get to know when he will be clicking on unsubscribe link, as I have a web application, so do I need to make application to get back? – Incredible Nov 08 '13 at 09:32
  • Also +1: for giving me an idea. :) – Incredible Nov 08 '13 at 09:32
  • @ItiTyagi: if user clicks on the given url(i mean in the solution) he will be redirected your your webpage from where you can proceed further by taking the emailid from the querystring. if not clarified please let me know. – Sudhakar Tillapudi Nov 08 '13 at 10:44
  • I have a secure application where only few staff have access, these staff send email to their customers, so cannot give them an access. I think I will need to make another short single page web appl. What say? – Incredible Nov 08 '13 at 10:50
  • @ItiTyagi: "can't give access" by you mean you can't even allow others to open single page from your website, if you are maintaining User Management it won't be a problem. – Sudhakar Tillapudi Nov 08 '13 at 10:54
  • 13
    This allows anyone to unsubscribe anyone else just by knowing their email address – thelem Mar 13 '17 at 16:58
  • 5
    @thelem, not only that. SQL-injection in example. How user with such rating can post answers like that? – dmytro.s Feb 15 '18 at 16:09
1

I guess you have your subscribers stored somewhere. So the easy way to unsubscribe is to remove the record from the store by its email-address.

It has nothing to do with the SmtpClient class.

Emil Lundin
  • 577
  • 5
  • 14
1

The concept of a mailing list includes a list of email addresses, usually saved in a database. Your code adds the receiver from an "EmailTxt", I guess a textbox?

  • As long as you do not save email addresses somewhere, there is no need to unsubscribe
  • If you save email addresses, then create another file that allows people to unsubcribe.

An unsubscribe file would usually function as follows:

  • receive the email address to unsubscribe as a querystring parameter
  • display this email address in a single textbox, along with an "unsubscribe" button
  • if the user clicks this button, then remove the email address from the database.
The Conspiracy
  • 3,495
  • 1
  • 17
  • 18