2014-09-29 73 views
0

我想做一個程序,它發送電子郵件到gmail服務器。我已經完成了我的目標,但現在我的下一個目標是創建一個登錄表單,我可以使用不同的Gmail帳戶登錄併發送郵件。如何從文本框訪問屬性到另一個表格

下面是該Login形式的代碼:

private void btnLogin_Click(object sender, EventArgs e) 
{ 
    User user = new User(); 
    user.Email = textBoxEmail.Text; 
    user.Password = textBoxPassword.Text; 
} 

我希望我的user.Emailuser.Password被保存在第二種形式(主要形式),它是這樣的:

private void btnSend_Click(object sender, EventArgs e) { 
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

    MailMessage mail = new MailMessage(); 
    mail.From = new MailAddress(/*I want user.Email to be here */,"Pavel Valeriu"); 
    mail.To.Add(textBoxTo.Text); 
    mail.Subject = textBoxSubject.Text; 
    mail.Body = richText.Text; 

    SmtpServer.Port = 587; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", /* and user.Password here */); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 

    MessageBox.Show("mail sent"); 
    Close(); 
} 
+0

這個問題是不相關的'email'標籤。我刪除它爲您 – 2014-09-29 10:23:15

回答

1

您可以從登錄表單通過構造函數傳遞價值的主要形式

FormLogin:

private void btnLogin_Click(object sender, EventArgs e) 
{ 
    User user = new User(); 
    user.Email = textBoxEmail.Text; 
    user.Password = textBoxPassword.Text; 
    FormMain frm = new FormMain (textBoxEmail.Text,textBoxPassword.Text); 
    frm.show(); 
} 

FormMain:

public FormMain(string _email,string _password) 
{ 
    InitializeComponent(); 
    Email = _email; 
    Password = _password; 
} 
string Email = sting.Empty; 
string Password = string.Empty; 

private void btnSend_Click(object sender, EventArgs e) 
{ 
    MailMessage mail = new MailMessage(); 
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

    mail.From = new MailAddress(Email ,"Pavel Valeriu"); 
    mail.To.Add(textBoxTo.Text); 
    mail.Subject = textBoxSubject.Text; 
    mail.Body = richText.Text; 
    SmtpServer.Port = 587; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", Password); 
    SmtpServer.EnableSsl = true; 
    SmtpServer.Send(mail); 
    MessageBox.Show("mail Send"); 
    Close(); 
} 

編輯: -

包含默認構造形式爲

public FormMain() 
{ 
    InitializeComponent(); 
} 
+0

感謝的很多,這正是我想要的! – Valeriu92 2014-09-29 10:34:53

+0

我做了這個,但現在我有這個錯誤「‘SendEmail.Form1’不包含一個構造函數參數0」 ...... – Valeriu92 2014-09-29 13:33:24

+0

包括默認的構造形式, – 2014-09-29 14:35:52

0

你可以定義static class UserInformation與字段static string Username & static string Password並以第一種形式

UserInformation.Username = "Sth"; 

,並在第二形式

youTextBox.Text = UserInformation.Username. 

瞭解詳情,請閱讀http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

+1

感謝您的答覆,但我不想與靜態值工作... – Valeriu92 2014-09-29 10:31:42

+0

郵件客戶端的概念需要這樣一個靜態類和價值,因爲你所需要的用戶名和密碼的任何地方.. 。 – 2014-09-29 10:38:53

0

你可以在聲明用戶classpublic static您先從:

public static class User... 
... 
private void btnLogin_Click(object sender, EventArgs e) 
{ 

    User.Email = textBoxEmail.Text; 
    User.Password = textBoxPassword.Text; 
} 

和主要形式訪問Form1的用戶屬性:

... 
mail.From =Form1.User.Email 
0

如果安全性和設計不是什麼大問題,而且您只是想讓它工作,那麼您可以繼續創建一個靜態類來爲您提供這些信息,比如數據提供者。

public static class LoginDataProvider 
{ 
    public static string Email {get; set;} 
    public static string Password {get; set;} 
} 

然後,您可以繼續獲得保存到您的登錄表單:

LoginDataProvider.Email = textBoxEmail.Text; 
LoginDataProvider.Password = textBoxPassword.Text; 

然後,您可以繼續前進,檢索第二種形式以同樣的方式的信息。

0

如果您正在ASP中開發應用程序。NET可以提到在「會話」其他明智的,你與窗口的應用程序的移動,你可以存儲在全局變量和訪問憑據的任何網絡形成

0

當您創建主窗體的對象,當用戶的用戶名和密碼認證,創建一個constructor通過用戶的電子郵件密碼&像

mainForm(string userEmail, string password) or mainForm(User user) 

所以

User user = new User(); 
user.Email = textBoxEmail.Text; 
user.Password = textBoxPassword.Text; 

If(user.Authenticate()) 
{ 
    //call new MainForm(user) 
相關問題