Solved

SMTP email - using variables for the To field

Posted on 2014-04-01
4
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 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