Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SMTP email - using variables for the To field

Posted on 2014-04-01
4
Medium Priority
?
157 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

972 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