Solved

SMTP email - using variables for the To field

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

20 Experts available now in Live!

Get 1:1 Help Now