Solved

SMTP email - using variables for the To field

Posted on 2014-04-01
4
147 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now