Solved

SMTP email - using variables for the To field

Posted on 2014-04-01
4
148 Views
Last Modified: 2014-04-02
Hi Experts,

Not sure if this is doable, but it seems so theoretically.

I want to trigger an email on a button click - that much I can do already.

However based on the identity of the user clicking the button ( whose identity is stored as a session variable) I need to vary the 'To' field.

So is there a way to set the MailMessage.From based on the userid stored in my session?
0
Comment
Question by:forsters
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39969808
Where are your users coming from? Are they aspnet membership users, AD users, or something else? In other words, do you have a way of retrieving the users email? If you are using AD integration, and Exchange, then you should find it will be able to translate "LastName, FirstName" to an actual user.
0
 

Author Comment

by:forsters
ID: 39969891
hi thanks for reply, this will be internal so I'm using the user log in 'username' which I can then verify against a sql table holding the corresponding emails. But not quite sophisticated enough to tie in with AD or Exchange.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39970066
In an very simplified example, you would do something like:
SqlConnection cn = new SqlConnection("Your connection string");
SqlCommand cmd = new SqlCommand("SELECT [Name], [Email] FROM [User] WHERE Username=@Username", cn);
cmd.Parameters.AddWithValue("@Username", Session["username"]);

cn.Open();
SqlDataReader rdr = cmd.ExecuteReader();

if (rdr.Read())
{
     string name = rdr.GetString(0);
     string email = rdr.GetString(1);

     MailAddress toAddress = new MailAddress(email, name);

     MailMessage msg = new MailMessage();
     msg.To = toAddress;

     // TODO: Populate rest of message and send
}

cn.Close(); 

Open in new window

0
 

Author Closing Comment

by:forsters
ID: 39971667
thank you, that makes sense and gives me a great place to start and mess about with a few tests. thanks very much.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question